class 的构造函数必须显式初始化基础 class
Constructor for class must explicitly initialize the base class
我有:
//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
的构造函数中出现错误:
Constructor for 'SpecificElement' must explicitly initialize the base
class 'Tree::Node' which does not have a default contructor
Node 不是应该通过 Element 初始化吗?为什么编译器要求我在那里显式初始化?
不知道这跟建设者被保护有没有关系。或者如果它是用于名称空间,虽然我不这么认为,因为代码编译后,直到我引入 class SpecificElement
.
我会在 SpecificElement
中调用 Node 的构造函数,但我有更多的 class 继承自此,并且都要求我显式初始化 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
}
};
}
Isn't Node
supposed to be initialized through Element
?, why the compiler ask me for explicitly initialize there?
没有
只有当您创建 Element
的实例时才会发生这种情况,而不是 Element
的子 class。这与 virtual
继承有关。
被 virtual
继承的 类 必须在最派生的 class 的构造函数中正确初始化。因此,您必须使用:
SpecificElement(const std::string name) : Tree::Node(Node::ELEMENT) {
nodename = name;
}
有了它,当您构造 SpecificElement
的实例时,Tree::Node
子对象仅从 SpecificElement
的构造中初始化一次。 Element
的构造函数中的 Tree::Node(Node::Element)
部分在运行时被忽略。
中的标准中找到此行为的理由
我有:
//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
的构造函数中出现错误:
Constructor for 'SpecificElement' must explicitly initialize the base class 'Tree::Node' which does not have a default contructor
Node 不是应该通过 Element 初始化吗?为什么编译器要求我在那里显式初始化?
不知道这跟建设者被保护有没有关系。或者如果它是用于名称空间,虽然我不这么认为,因为代码编译后,直到我引入 class SpecificElement
.
我会在 SpecificElement
中调用 Node 的构造函数,但我有更多的 class 继承自此,并且都要求我显式初始化 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
}
};
}
Isn't
Node
supposed to be initialized throughElement
?, why the compiler ask me for explicitly initialize there?
没有
只有当您创建 Element
的实例时才会发生这种情况,而不是 Element
的子 class。这与 virtual
继承有关。
virtual
继承的 类 必须在最派生的 class 的构造函数中正确初始化。因此,您必须使用:
SpecificElement(const std::string name) : Tree::Node(Node::ELEMENT) {
nodename = name;
}
有了它,当您构造 SpecificElement
的实例时,Tree::Node
子对象仅从 SpecificElement
的构造中初始化一次。 Element
的构造函数中的 Tree::Node(Node::Element)
部分在运行时被忽略。