我有一个代表树的基类。
class BaseTree
{
protected:
class Node
{
public:
Node* left()
{ return children[0]; }
Node* right()
{ return children[1]; }
protected:
std::vector<Node*> children;
};
Node* root;
public:
Node* get_root()
{ return root; }
};
我需要创建另一个继承自DerivedTree
的类BaseTree
,并在DerivedTree
内部创建一个继承自DerivedTree::Node
的嵌套类BaseTree::Node
像这样:
class DerivedTree : public BaseTree
{
protected:
class Node : public BaseTree::Node
{
public:
Node* third_node()
{ return (Node*)children[2]; }
};
};
int main()
{
DerivedTree* tree = new DerivedTree();
tree->get_root()->third_node(); // ERROR: ‘class BaseTree::Node’ has no member named ‘third_node’
}
问题是,在DerivedTree
和DerivedTree::Node
中,继承的成员函数和继承的变量root
的返回类型将是BaseTree::Node*
。
有没有办法在DerivedTree
,DerivedTree::Node
而不是BaseTree::Node
中设置返回类型?