类和嵌套类

时间:2017-05-25 19:37:50

标签: c++ templates inheritance

我有一个代表树的基类。

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’
}

问题是,在DerivedTreeDerivedTree::Node中,继承的成员函数和继承的变量root的返回类型将是BaseTree::Node*

有没有办法在DerivedTreeDerivedTree::Node而不是BaseTree::Node中设置返回类型?

0 个答案:

没有答案