How to avoid boilerplate code constructors?
c++, inheritance, oop
Solution
In a C++11 compliant compiler (§12.9 in the standard), you can actually do this quite easily:
struct Son : public Base {
using Base::Base;
};
This inherits all constructors from `Base` and is equivalent to your code for class `Son`.
Problem
Let's have class like this: ``` struct Base { Base() { ... } Base(int) { ... } Base(int,string) { ... } ... }; ``` I'd like to inherit many classes from `Base`, so I write ``` struct Son : public Base { Son() : Base() { } Son(int) : Base(int) { } Son(int,string) : Base(int,string) { } }; struct Daughter : public Base { Daughter() : Base() { } Daughter(int) : Base(int) { } Daughter(int,string) : Base(int,string) { } }; ``` and I don't need to add any code to child's constructors. Is it possible to inherit them implicitly? To call them the same way like in `Base`, just change the name? Preprocessor can be abused here, but is there any other workaround?