Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

//The base class:   class BaseClass  {  public:      BaseClass(std::string);     BaseClass(const BaseClass& orig);     virtual ~BaseClass();     const std::string GetData() const;     void SetData(const std::string& data); protected:      BaseClass();     std::string m_data;  };  BaseClass::BaseClass(const std::string data) : m_data(data) { }  BaseClass::BaseClass() { }   BaseClass::BaseClass(const BaseClass& orig) { }  BaseClass::~BaseClass() { }  void BaseClass::SetData(const std::string& data) {     m_data = data; }  const std::string BaseClass::GetData() const {     return m_data; }   //The derived class:    class DerivedClass : public BaseClass {  public:      DerivedClass(std::string data);     DerivedClass(const DerivedClass& orig);     virtual ~DerivedClass(); private:  };  DerivedClass::DerivedClass(std::string data) : m_data(data) { } //ERROR HERE  DerivedClass::DerivedClass(const DerivedClass& orig) { }  DerivedClass::~DerivedClass() { } 

//The compiler error

DerivedClass.cpp:3: error: class ‘DerivedClass’ does not have any field named ‘m_data’

Any help is greatly appreciated. Thank you in advance.

like image 659
dwinchell Avatar asked Jul 23 '10 15:07

dwinchell


People also ask

Can base class access protected members of derived class?

Protected members in a class are similar to private members as they cannot be accessed from outside the class. But they can be accessed by derived classes or child classes while private members cannot.

How does a derived class can get access privilege for a private member of the base class?

Private members of the base class cannot be used by the derived class unless friend declarations within the base class explicitly grant access to them. In the following example, class D is derived publicly from class B . Class B is declared a public base class by this declaration.

How do you access base class members in a derived class?

A base class's private members are never accessible directly from a derived class, but can be accessed through calls to the public and protected members of the base class.

How are protected member of a base class access in the derived class when inherited privately in C Plus Plus?

If a class is derived privately from a base class, all protected base class members become private members of the derived class. Class A contains one protected data member, an integer i . Because B derives from A , the members of B have access to the protected member of A .


1 Answers

You cannot initialize m_data in the derived class constructor but instead pass it as an argument to the base class constructor.

That is: DerivedClass::DerivedClass(std::string data) : BaseClass(data) { }

like image 71
Zitrax Avatar answered Oct 04 '22 15:10

Zitrax