![](/img/trans.png)
[英]How would you the "this" pointer of the base class from a method of a nested class?
[英]How to return nested class pointer from method?
我有两个类: Database
和Node
的嵌套类,是有可能有一个方法Node
将返回一个Node*
?
我试图将方法nextNode
返回类型设置为Node*
但是出现编译错误: 'Node' does not name a type
数据库
class Database
{
public:
Database();
Database& operator+=(const Client&);
~Database();
private:
class Node //node as nested class
{
public:
Node(); //ctor
void setHead(Client*&); //add head node
Node* nextNode(Node*&); //return new node from the end of he list
private:
Client* data; //holds pointer to Client object
Node* next; //holds pointer to next node in list
};
Node *head; //holds the head node
};
Databse.cpp中的nextNode
方法声明:
Node* Database::Node::nextNode(Node*& start)
{
....
....
return current->next;
}
谢谢。
Node
嵌套在Database
,因此您需要返回类型的范围:
DataBase::Node* Database::Node::nextNode(Node*& start)
^^^^^^^^^^
该参数已在范围内,因此您可以保持原样。
除了juanchopanza的答案,C ++ 11的尾随返回类型还允许您在范围内声明它:
auto Database::Node::nextNode(Node*& start) -> Node* {
// ...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.