且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从成员访问受保护的/私有的嵌套类指针

更新时间:2023-02-15 15:19:07

要从类外部访问myQueue::Node,您需要稍微重写一下getter函数:

To acess myQueue::Node from outside the class you need to rewrite your getter function a bit:

template<class T>
myQueue<T>::Node* myQueue<T>::getHead()
{
    return head;
}

然后您可以像这样在main()中使用它

Then you can use it in main() like this

auto head = queue.getHead();

请注意,在这种情况下,auto的用法很重要.您仍然不能在myQueue<T>之外声明任何myQueue<T>::NodemyQueue<T>::Node**类型的变量,但是可以使用auto变量来保存这些类型.

Note that the usage of auto is important in this case. You still cannot declare any variable of type myQueue<T>::Node or myQueue<T>::Node** outside of myQueue<T>, but you can use auto variables to hold these types.