Passing a variable to a constructor in c++

Viewed 113

I'm getting started with class inheritance in c++ and I'm really stuck.

From the child class I'm trying to call the parent constructor and pass a variable as a parameter. The warning says field 'default_name' is uninitialized when used here [-Wuninitialized].

class ParentClass {

    public:
        ParentClass(std::string _name): name(_name) {};

        std::string name;

        void printname() {
            std::cout << this->name << std::endl;
        }

}

class DerivedClass: public ParentClass {

    public:

         std::string default_name = "Alex";

         DerivedClass(): ParentClass(default_name) {};

}

This obviously does not work because DerivedClass() is going to be called before default_nameis even initialized. Is there a way to construct an object of the parent class from the derived class using a variable?

3 Answers

Parent classes are constructed first. Even though, in your program, your initialization of name appears first, that doesn't mean anything. C++ has strict construction and initialization rules. You are attempting to construct the parent class before name gets initialised.

name gets constructed after the parent class gets constructed.

So what you can do is to move name into another parent class before the existing one. That one gets constructed first, then your existing constructor will have an initialized member, to work with..

Members are always initialized in the same order: First base class sub-objects, then members in the order they are listed in the class declaration. Hence, your usage of a default initializer is more or less the same as:

class DerivedClass: public ParentClass {
    public:
         std::string default_name;
         DerivedClass(): ParentClass(default_name),default_name("Alex") {};
}

Now it is more obvious that default_name is passed to the constructor of ParentClass before it is initialized. I think you already knew that... to fix it you have several options. As the default_name seems to be attached to the DerivedClass rather than instances of the class, you can make it static:

#include <string>
#include <iostream>

class ParentClass {    
    public:
        ParentClass(std::string _name): name(_name) {};    
        std::string name;    
        void printname() {
            std::cout << name;
        }    
};

class DerivedClass: public ParentClass {    
    public:    
         static const std::string default_name;    
         DerivedClass(): ParentClass(default_name) {};    
};

const std::string DerivedClass::default_name = "Alex";

int main(){
    DerivedClass{}.printname();
}

A base class has to be initialized before the current class members, hence the error. Here, you could make default_name static. That way it would be available at object construction time:

class DerivedClass: public ParentClass {

    public:

         static std::string default_name;

         DerivedClass(): ParentClass(default_name) {};

};

std::string DerivedClass::default_name = "Alex";

Here, default_name could (should?) be const.But in fact, it would be even simpler to use a literal string here:

class DerivedClass: public ParentClass {

    public:

         DerivedClass(): ParentClass("Alex") {};

};
Related