35

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.

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
dwinchell
  • 405
  • 1
  • 4
  • 6

6 Answers6

54

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) { }

Zitrax
  • 19,036
  • 20
  • 88
  • 110
10

In the initializer list you can just set values for attributes of the same class. To access it, you must attribute the value in the body of the constructor:

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

Or, if it is expensive to copy the object, you pass the m_data as an argument to the parent class constructor :

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

Tip: Pass your data as a reference to prevent the copy constructor.

See more info here: order of initialization of C++ constructors.

neves
  • 33,186
  • 27
  • 159
  • 192
  • 1
    Assigning members in the body of the constructor should be discouraged, so I would consign that to a footnote at most. Also, nowadays that string should be `std::move()d` into the member or base class in order to avoid a pointless copy. – underscore_d Sep 16 '18 at 20:06
4

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;
}
James Curran
  • 101,701
  • 37
  • 181
  • 258
  • Sure, they *can* do that, but they probably shouldn't and don't *really* want to! Assigning members in the body of the constructor should be discouraged, and I think that should be especially emphasised if it's a base class's member! This will waste resources assigning a value that gets changed shortly after, introduce an intermediate state that gets discarded between the init-list of the base and the constructor body of the derived class, etc. It's just best avoided. – underscore_d Sep 16 '18 at 20:08
2

You need to call the base class constructor as follows:

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

Each class should be in charge of initializing it's members.

code-gijoe
  • 6,949
  • 14
  • 67
  • 103
1

Initializer lists can only be used to initialize fields which are owned by the type in question. It is not legal to initialize base class fields in an initializer lists which is why you receive this error. The field is otherwise accessible within DerivedClass

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
0

Your derived class constructor does have access, but you cannot assign to it in the initialisation list. Change the constructor to:

DerivedClass::DerivedClass(const std::string& data)
 : BaseClass(data)
{
}

Alternatively, if no suitable base class constructor is available and you cant add one you can change the constructor to:

DerivedClass::DerivedClass(const std::string& data)
{
    m_data = data;
}
Will Manley
  • 2,340
  • 22
  • 17