"methodX() is a protected member of MyClass" error inside class that extends MyClass

75 views Asked by At

In the following code:

struct A{
protected:
    virtual void IAmProtected() = 0;
};

struct B: public A{
private:
    A * a;
protected:
    void IAmProtected() override;
    
public:
    
    void doSomething(){
        a->IAmProtected(); // ERROR: 'IAmProtected' is a protected member of 'A'
    }
};

I get 'IAmProtected' is a protected member of 'A' which I find to be strange since B is in fact an extension of A.

I want to know

  1. Why does this happen?
  2. How can I prevent this?
0

There are 0 answers