What is the order of constructor calling in C++ during multiple inheritance ?

Answer1

 

Base class constructors are automatically called for you if they have no argument. If you want to call a superclass constructor with an argument, you must use the subclass's constructor initialization list. 

 

class SuperClass
{
    public:

        SuperClass(int X)
        {
            // do something with foo
        }


};

class SubClass : public SuperClass
{
    public:

        SubClass(int X, int Y)
        : SuperClass(X)    // Call the superclass constructor in the subclass' initialization list.
        {
            // do something with Y
        }
};

Answered by: RAJESH HEGDE     On: 23-Mar-2012


Dear Guest,
Spend a minute to Register in a few simple steps, for complete access to the Social Learning Platform with Community Learning Features and Learning Resources.
If you are part of the Learning Community already, Login now!