Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using C++ base class constructors?

While working with templates I ran into a need to make a base class constructors accessible from inherited classes for object creation to decrease copy/paste operations. I was thinking to do this through using keyword in same manner with functions case, but that not work.

class A { public:      A(int val) {} };  class B : public A { };  class C : public A { public:     C(const string &val) {} };  class D : public A { public:     D(const string &val) {}     using A::A;              // g++ error: A::A names constructor };  void main() {     B b(10);                // Ok.   (A::A constructor is not overlapped)     C c(10);                // error: no matching function to call to 'C::C(int)' } 

So my question: Is there any way to import a base class constructors after new ones in inherited class been declared?

Or there is only one alternative to declare new constructors and call a base ones from initializer list?

like image 498
minyor Avatar asked Nov 11 '11 12:11

minyor


People also ask

How do you use a base class constructor?

How to call the parameterized constructor of base class in derived class constructor? To call the parameterized constructor of base class when derived class's parameterized constructor is called, you have to explicitly specify the base class's parameterized constructor in derived class as shown in below program: C++

How do you call a base class constructor?

A derived Java class can call a constructor in its base class using the super keyword. In fact, a constructor in the derived class must call the super's constructor unless default constructors are in place for both classes.

Do you have to call base class constructor?

If a class do not have any constructor then default constructor will be called. But if we have created any parameterized constructor then we have to initialize base class constructor from derived class. We have to call constructor from another constructor. It is also known as constructor chaining.

Can base class have constructor?

base (C# Reference)A base class access is permitted only in a constructor, an instance method, or an instance property accessor. It is an error to use the base keyword from within a static method. The base class that is accessed is the base class specified in the class declaration.


2 Answers

Yes, Since C++11:

struct B2 {     B2(int = 13, int = 42); }; struct D2 : B2 {     using B2::B2; // The set of inherited constructors is // 1. B2(const B2&) // 2. B2(B2&&) // 3. B2(int = 13, int = 42) // 4. B2(int = 13) // 5. B2()  // D2 has the following constructors: // 1. D2() // 2. D2(const D2&) // 3. D2(D2&&) // 4. D2(int, int) <- inherited // 5. D2(int) <- inherited }; 

For additional information see http://en.cppreference.com/w/cpp/language/using_declaration

like image 155
Sergei Krivonos Avatar answered Oct 17 '22 12:10

Sergei Krivonos


Prefer initialization:

class C : public A { public:     C(const string &val) : A(anInt) {} }; 

In C++11, you can use inheriting constructors (which has the syntax seen in your example D).

Update: Inheriting Constructors have been available in GCC since version 4.8.


If you don't find initialization appealing (e.g. due to the number of possibilities in your actual case), then you might favor this approach for some TMP constructs:

class A { public:      A() {}     virtual ~A() {}     void init(int) { std::cout << "A\n"; } };  class B : public A { public:     B() : A() {}     void init(int) { std::cout << "B\n"; } };  class C : public A { public:     C() : A() {}     void init(int) { std::cout << "C\n"; } };  class D : public A { public:     D() : A() {}     using A::init;     void init(const std::string& s) { std::cout << "D -> " << s << "\n"; } };  int main() {     B b; b.init(10);     C c; c.init(10);     D d; d.init(10); d.init("a");      return 0; } 
like image 34
justin Avatar answered Oct 17 '22 12:10

justin