如何访问另一个类中的内部类

How do you access inner classes within another class?

本文关键字:内部类 另一个 何访问 访问      更新时间:2023-10-16

我在另一个类中有一个节点类,该类在结构类中是私有的,这是我正在使用的主要类。然后,我在另一个。cpp文件中实现了Structure .h文件中的函数,该文件包含了Structure类和Node类,嵌套在Structure类中的private。

在创建Node*的返回类型时如何访问该Node类?我已经摆弄了一段时间了,但什么也不管用。它允许我返回int, bool, double类型,但不能返回Node*。

"Structure.h"
template <class T> 
class Structure
{
    public:
        // Structure functions();
    private:
    class Node
{
    public::
        // Node functions and variables;
}
    // Structure functions_private();
}

你的意思是:

template< class T >
typename Structure<T>::Node* Structure<T>::MyMemberFunction()
{
    return new Node();
}

举个例子

你还需要把它放在你的头文件中,而不是在源代码中

在类定义中,您可以简单地将该类型称为Node

外部,您需要限定名,如果它依赖于模板参数,则需要typename:

Structure<int>::Node * some_function();  // not dependent, no typename
template <typename T>
typename Structure<T>::Node * some_template(); // dependent, needs typename