2

I am trying to understand why 'return new A(x)' is giving us an error saying " ‘A::A(int)’ is protected within this context ".

I am aware that protected member functions can be accessed inside a derived class.

Can someone please help me understand why I am not allowed to 'return new A(x)' inside the derived class (class B) as the constructor is protected in the base class (class A).

class A {
  public:
    A() { val = 0; }
    virtual ~A() {}
  protected:
    A(int x) { val = x; }
  private:
    int val;
};


class B: public A {
  public:
    B(): A(0) {}
    A *createA(int x = 0) { return new A(x); }
}; 

0 Answers0