【发布时间】:2015-06-21 14:41:31
【问题描述】:
我有一个类层次结构,可以归结为
class Module { };
struct Port {
Module& owner;
Port(Module& owner) : owner(owner) {}
};
struct InPort : virtual Port { using Port::Port; };
struct OutPort : virtual Port { using Port::Port; };
struct InOutPort : InPort, OutPort { using Port::Port; };
如您所见,我更愿意创建一些基本功能,并以经典的菱形图案继承它。我还想使用构造函数继承来使其成为未来的证据......
但是,this does not work as written down above
prog.cpp: In function 'int main()':
prog.cpp:14:15: error: use of deleted function 'InOutPort::InOutPort(Module&)'
InOutPort p(m);
甚至将InOutPort 的定义替换为更明确的版本is not enough:
struct InOutPort : InPort, OutPort { InOutPort(Module& m) : Port(m), InPort(m), OutPort(m) { } };
改为I seem to have to write down everything explicitly for it to work::
struct InPort : virtual Port { InPort(Module& m) : Port(m) { } };
struct OutPort : virtual Port { OutPort(Module& m) : Port(m) { } };
struct InOutPort : InPort, OutPort { InOutPort(Module& m) : Port(m), InPort(m), OutPort(m) { } };
有没有办法将构造函数继承与我忽略的虚拟继承结合起来?
如果没有,您会使用什么替代方案?
也许可变参数模板构造函数可以完美地将其参数转发到所有基础?
【问题讨论】:
-
InPort和OutPort都继承了一个构造函数,该构造函数为Port调用未声明的默认构造函数,因为它们都不是派生最多的类。如果调用它们,该程序将是格式错误的。正因为如此,gcc 决定删除两个类中相应的构造函数。即使基数是initialized explicitly,Clang 也不会给出关于构造函数的错误。顺便说一句,clang 也给出了一个错误,因为Port在 using 声明中不是InOutPort的直接基类,而 gcc 会忽略它。 -
GCC 实际上确实删除了构造函数,因为它们的格式不正确,但不是因为我之前推测的原因。似乎使用声明implicitly odr-uses the default-constructor。
Port类中的默认构造函数未声明,因此它删除了调用构造函数。这是一个错误。 -
另外,只有当它是一个虚拟基类时才会发生。
标签: c++ c++11 inheritance virtual-inheritance