【发布时间】:2015-04-25 16:11:12
【问题描述】:
我一直在使用继承在 C++ 中实现红黑树。我有 4 个类,节点、树、RBNode、RBTree。
class Node
{
protected:
int data;
Node *left;
Node *right;
Node *parent;
public:
Node();
Node(int data);
void print_node(ofstream &file);
Node * find_node(int data);
void insert_node(Tree *t);
void left_rotate_node(Tree *t);
void right_rotate_node(Tree *t);
void delete_node(Tree *t);
}
class Tree
{
protected:
Node * root;
list<int> treedata;
public:
Tree();
virtual Node * get_root();
virtual void set_root(Node *root_node);
void insert_into_tree();
void delete_from_tree();
virtual void print_tree();
}
RBNode 和 RBTree 分别继承 Node、Tree。但我无法使用 Node 类的功能。比如函数void Tree::insert_node(Tree *t);
即使在类 RBNode 中,这个函数也做同样的工作,只是函数接收 RBTree 作为参数。如何在不在 RBNode 中重新声明它的情况下使用相同的功能。我想过在函数内部使用强制转换,但是我怎么知道哪些类对象正在调用该函数。
请给我一些建议。我是 C++ 新手。
【问题讨论】:
标签: c++ inheritance