【发布时间】:2019-12-24 07:43:11
【问题描述】:
我有:
//all classes are in different files
namespace Tree {
class Node {
NodeType type;
protected:
std::string nodename;
Node(Node::NodeType _type) : type(_type) {}
};
}
class Parent : public virtual Tree::Node {
protected:
Parent() : Node(Node::UNDEFINED) {}
};
class Child: public virtual Tree::Node {
protected:
Child() : Node(Node::UNDEFINED) {}
};
namespace Tree {
class Element :
public Parent,
public Child,
public virtual Node {
protected:
Element() : Node(Node::ELEMENT) {}
};
}
class SpecificElement: public Tree::Element {
public:
SpecificElement(const std::string name) {
nodename = name;
}
};
SpecificElement的构造函数中出现错误:
“SpecificElement”的构造函数必须显式初始化基 没有默认构造函数的类“Tree::Node”
Node不应该通过Element初始化吗?为什么编译器要我显式初始化那里?
我不知道这是否与受保护的建筑商有关。或者如果是用于命名空间,虽然我不这么认为,因为代码编译后,直到我引入了 SpecificElement 类。
我会在 SpecificElement 中调用 Node 的构造函数,但是我有更多的类继承自它,并且总要求我显式初始化 Node,而我无法按设计这样做。
编辑: 感谢@r-sahu,我解决了如下
namespace Tree {
class Node {
protected:
std::string nodename;
NodeType type; //changed to protected
Node(Node::NodeType _type) : type(_type) {}
Node() : Node(UNDEFINED) {} //default constructor
};
}
namespace Tree {
class Element :
public Parent,
public Child,
public virtual Node {
protected:
Element() { //change the init constructor list
type = Node::ELEMENT; //change type inside constructor
}
};
}
【问题讨论】:
-
Element不应该显式调用节点的Ctor吗?
-
不需要,因为现在Node有
default构造函数
标签: c++ inheritance constructor namespaces multiple-inheritance