Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any way to use base constructor in derived class?

After using C# for the past decade or two, my C++ has gone a little rusty.

If I have the following:

class CBase
{
public:
    CBase(LPCTSTR pszArg1, LPCTSTR pszArg2, LPCTSTR pszArg3);
    virtual ~CBase();

    // Etc...
};

class CDerived : CBase
{
    // Etc...
};

It appears I cannot create an instance of CDerived.

no instance of constructor "CDerived::CDerived" matches the argument list

I know I can create a derived constructor explicitly:

CDerived::CDerived(LPCTSTR pszArg1, LPCTSTR pszArg2, LPCTSTR pszArg3)
    : CBase(pszArg1, pszArg2, pszArg3)
{
}

But that seems like a lot of typing, particularly if I plan to derive many classes from the base class.

The base class still needs those arguments one way or another. Is there a way to not have to rewrite this arguments for every derived class, "exposing" the base constructor perhaps, or must I absolutely always do as I've done above?

like image 515
Jonathan Wood Avatar asked Dec 08 '16 03:12

Jonathan Wood


People also ask

What is the function of the derived class constructor?

When constructing a derived class, the derived class constructor is responsible for determining which base class constructor is called. If no base class constructor is specified, the default base class constructor will be used.

What is the difference between base class and derived class?

Base class constructors are always called using the derived class constructors. Whenever you create derived class object, first the base class default constructor is executed and then the derived class’s constructor finishes execution. In inheritance constructor of base class is inherited like other member functions.

Can We explicitly call the base class constructor in the constructor?

What's more, we can explicitly call the base class constructor in the child class constructor. A base class is also called a " superclass ". That's why Java uses the keyword super to indicate the base class. In the previous example public Cat(String brain, String heart, String tail) { this. brain = brain; this. heart = heart; this. tail = tail; }

What is the difference between derived and Base Constructors?

All that’s happening is that the Derived constructor is calling a specific Base constructor to initialize the Base portion of the object. Because m_id lives in the Base portion of the object, the Base constructor is the only constructor that can initialize that value.


2 Answers

You could use inheriting constructors (since C++11):

class CDerived : public CBase
{
public:
    using CBase::CBase;
    // Etc...
};

Then you could

LPCTSTR pszArg1;
LPCTSTR pszArg2;
LPCTSTR pszArg3;
CDerived d(pszArg1, pszArg2, pszArg3); // initialize CBase subobject by CBase::CBase(LPCTSTR, LPCTSTR LPCTSTR), 
                                       // then default-initialize other members of CDerived
like image 200
songyuanyao Avatar answered Sep 30 '22 06:09

songyuanyao


Yes, you can do that in C++11 and later. To inherit a base constructor, you must use the using keyword followed by the name of the base class constructor:

struct CBase {
    CBase(LPCTSTR pszArg1, LPCTSTR pszArg2, LPCTSTR pszArg3);
    virtual ~CBase();

    // Etc...
};

struct CDerived : CBase {
    // we use the base constructor
    using CBase::CBase;
};
like image 25
Guillaume Racicot Avatar answered Sep 30 '22 06:09

Guillaume Racicot