C++继承的类设计问题
C++ class design issue on inheritance
我继承了一些为Linux和Mac OS开发的代码。现在,我正在使用Visual Studio将其移植到Windows上。当我尝试使用Visual Studio 2010构建它时,我得到了下面列出的错误。 下面报告了简化类的结构。
考虑Node
类:
class Node
{
public:
Node();
virtual ~Node(){}
virtual bool isLeaf() const = 0;
/* other methods */
virtual vector<Node *>& getNodeList() const=0;
};
其中isLeaf
和getNodeList
是纯虚拟方法。
然后LeafNode
两个派生类
class LeafNode : public Node
{
public:
LeafNode(){ cout << "leaf constructor";}
~LeafNode(){ cout << "leaf destructor";}
bool isLeaf() const { return true; }
vector<Node *>& getNodeList() const {}
};
它只实现了isLeaf
方法,CompositeNode
:
class CompositeNode : public Node
{
public:
CompositeNode(){ cout << "CompositeNode constructor";}
~CompositeNode(){ cout << "CompositeNode destructor";}
bool isLeaf() const { return false;}
vector<Node *>& getNodeList() const{
return m_NodeList;
}
private:
vector<Node*> m_NodeList
};
它实现了两种纯虚拟方法。 如果我尝试使用Visual Studio构建此代码,则会出现以下错误:
error C4716: 'LeafNode::getNodeList()' : must return a value
我了解编译器,但我不知道如何处理这种情况。 我应该如何重新设计类来解决这个问题? 多谢。
我了解编译器,但我不知道如何处理这种情况。我应该如何重新设计类来解决这个问题?
LeafNode::getNodeList
只能返回一个空列表。将其实现为:
vector<Node *>& getNodeList() const
{
static vector<Node *> empty;
return empty;
}
也
virtual void isLeaf() const = 0;
是不对的。使用更有意义:
virtual bool isLeaf() const = 0;
相关文章:
- 混合组合和继承的C++问题
- 运算符继承和 cpp 核心准则 c.128 的问题
- 类继承,ENUM 与 AST 类实现的问题
- C++ 多级虚拟继承编译问题
- 如何解决此问题(基础模板和继承)
- 模板继承问题
- C++ 继承向量问题(无限循环+在其他类中使用向量的问题)
- 我是否需要在虚拟继承类的构造函数中初始化基类以解决菱形继承问题?
- 继承此C++代码有什么问题
- 一个关于继承和引用的C++问题
- C++继承和"常量"交互的问题
- 在钻石问题的求解中,为什么要虚拟地继承两次grand-parent类
- C++的抽象类继承和构造函数的问题
- C++:继承和列表函数作为类成员的问题
- SIGSEGV 因为基/派生类的继承问题
- 关于异常继承中的语法的问题
- C++继承的类设计问题
- 关于继承和覆盖的问题
- C++ 构造函数问题 // 继承的类
- 对复制构造函数的c++ OOP程序问题?继承