Cannot access protected member of base class in derived class

后端 未结 4 741
渐次进展
渐次进展 2021-01-07 19:01

I have the following code:

struct A {
protected:
    A() {}

    A* a;
};

struct B : A {
protected:
    B() { b.a = &b; }

    A b;
};

4条回答
  •  迷失自我
    2021-01-07 19:25

    It seems like a big limitation of the C++ language. How would you solve problem like this:

    class Node
    {
    public:
     void Save();
    protected:
     virtual void SaveState(int type) = 0;
    };
    
    class BinaryNode : public Node
    {
    protected:
     Node *left;
     Node *right;
    
     virtual void SaveState(int type) override
     {
        left->SaveState(type);
        right->SaveState(type);
     }
    };
    

    In this example I do not want to make method SaveState visible outside Node hierarchy. Only method Save should be public.

提交回复
热议问题