Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I need to reimplement all the constructor of a base class even if a derived class has no member variables?

Let's assume I have a base class

class Base
{
public:
    Base();
    Base(int i, double j);
    Base(int i, double j, char ch);

    virtual void print();

private:
    int m;
    double l;
    char n;
};

And I want to derive a class which overrides the print function but apart from that is exactly the same as the base class.

class Derived : public Base
{
public:
    void print();
};

Is there a way I can use all the constructors of the base class on the derived class without me rewriting all of them for the Derived class?

like image 731
RDGuida Avatar asked Dec 15 '22 13:12

RDGuida


1 Answers

Since C++11, you may use using for that:

class Derived : public Base
{
public:
    using Base::Base; // use all constructors of base.

    void print() override;
};

Live demo

like image 120
Jarod42 Avatar answered May 30 '23 09:05

Jarod42