简体   繁体   中英

Declare 'virtual' constructor for base and derived class in c++?

Maybe this is a trivial problem: I have a base class B which has a constructor with many (6+) arguments (and it's not stable maybe change in the future). And there are many derived classes inherit from it which also has the same constructor signature, and all of them just do nothing more than the base one.

So the problem is: is there a way to just declare one time in the base and no need to declare in derived classes? Just like a normal virtual function.

I'm using c++11.

You can inherit all the base class constructors:

struct base {
    base(int, float, void*, bool, std::nullptr_t, std::size_t) { /* ... */ }
};

struct derived : base {
    using base::base;
};



derived d(0,0.0f, nullptr, false, nullptr, 0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM