C++: Why does my DerivedClass's constructor not have access to the BaseClass's protected field?

前端 未结 6 997
执笔经年
执笔经年 2020-12-14 06:14

I have a constructor attempting to initialize a field in a base class. The compiler complains. The field is protected, so derived classes should have access.



        
6条回答
  •  我在风中等你
    2020-12-14 07:04

    You are not "accessing" m_data -- you are initializing it. However, it's already been initialized in the Base Class's ctor. If you want to change it's value, assign to it in the body of your ctor:

    DerivedClass::DerivedClass(std::string data) 
    {
       m_data = data;
    }
    

提交回复
热议问题