<分区>
我在重写派生类中的基类函数时遇到以下错误。
./inc/rbtree.h:16:18: error: invalid covariant return type for ‘virtual RBNode* RBTree::get_root()’
./inc/tree.h:25:24: error: overriding ‘virtual Node* Tree::get_root()’
rbtree.h
class RBNode;
class RBTree: public Tree
{
protected:
public:
RBTree();
RBNode *root;
RBNode * get_root();
void insert_into_tree();
//void delete_from_tree();
};
tree.h如下所示
class Node;
class Tree
{
protected:
Node * root;
list<int> treedata;
public:
Tree();
/* This is where it gives error */
virtual Node * get_root();
void set_root(Node *root_node);
void insert_into_tree();
void delete_from_tree();
void print_tree();
};
更多信息:RBNode 派生自 Node。 现在,我读到这个 article它说完全可以覆盖基类函数以返回派生类型实例。
我也在 SO 上进行了搜索,但有关此的问题并没有多大帮助。如果在 C++ 中允许,为什么会失败。