How to avoid template constructors?

Assume a class like this:

struct Base { Base() { ... } Base(int) { ... } Base(int,string) { ... } ... }; 

I would 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 code to child constructors. Is it possible to inherit them implicitly? To call them the same way as in Base , just change the name? The preprocessor can be abused here, but is there any other workaround?

+6
source share
1 answer

In a compiler compatible with C ++ 11 (Β§12.9 in the standard), you can do this quite easily:

 struct Son : public Base { using Base::Base; }; 

This inherits all the constructors from Base and is equivalent to your code for the Son class.

Unfortunately, it seems that most popular compilers (such as GCC or VC ++) do not yet support this feature, so for now you are out of luck. But try this code in your compiler and see what happens.

+7
source

Source: https://habr.com/ru/post/916443/


All Articles