0

我有一个抽象基类,它需要一些对象传递给它的构造函数来初始化它的成员。但我想摆脱通过派生类构造函数传递这些对象。

class Derived : public Base
{
public:
    Derived(type one, type two, type three) : Base(one, two, three)
    {
        // ...

传递给基类的对象对于所有创建的派生类都是相同的实例。有没有办法将它们绑定到基类的构造函数,这样我就不必通过派生类的构造函数转发它们?

// do some magic
// ...

class Derived : public Base
{
    // no need for an explicit constructor anymore
    // ...
4

1 回答 1

1

In C++11 you can inherit constructors to the base class. It seems this would roughly do what you need:

class derived
    : public base {
public:
    using base::base;
    // ...
};
于 2013-08-18T17:26:43.087 回答