Protected data in parent class not available in child class?

前端 未结 4 812
暖寄归人
暖寄归人 2020-12-03 17:37

I am confused: I thought protected data was read/writable by the children of a given class in C++.

The below snippet fails to compile in MS Compiler

         


        
4条回答
  •  暖寄归人
    2020-12-03 18:16

    You just shouldn't copy an A object in a B constructor. The intention is to leave the initialization of A's members to it's own constructor:

    struct A { 
      A( const A& a ): data( a.data ) {}
      protected: int data; 
    };
    
    struct B : public A {
      B( const A& a ): A( a ) {}
    };
    

提交回复
热议问题