在 C++ 中引用指针成员函数
我正在编写一个使用成员变量指针作为迭代器的成员函数。不过,我想纯粹为了可读性而引用函数内的指针。就像这样:
/* getNext will return a pos object each time it is called for each node
* in the tree. If all nodes have been returned it will return a Pos
* object (-1, -1).
* TODO: Add a lock boolean to tree structure and assert unlocked for
* push/pop.
*/
Pos BTree::getNext () const
{
BTreeNode*& it = this->getNextIter;
while (it)
{
if (it->visited)
{
/* node has been visited already, visit an unvisited right
* child node, or move up the tree
*/
if ( it->child [BTREE_RIGHT] != NULL
&& !it->child [BTREE_RIGHT]->visited)
{
it = it->child [BTREE_RIGHT];
}
else
{
it = it->parent;
}
}
else
{
/* if unvisited nodes exist on the left branch, iterate
* to the smallest (leftmost) of them.
*/
if ( it->child [BTREE_LEFT] != NULL
&& !it->child [BTREE_LEFT]->visited)
{
for (;
it->child [BTREE_LEFT] != NULL;
it = it->child [BTREE_LEFT]) {}
}
else
{
it->visited = 1;
return it->pos;
}
}
}
it = this->root;
this->setTreeNotVisited (this->root);
return Pos (-1, -1);
}
这基本上就是我想要的,其中 this->getNextIter 是一个 BTreeNode*。但是我收到错误:
btree.cpp:238: error: invalid initialization of reference of type
'DataTypes::BTreeNode*&' from expression of type 'DataTypes::BTreeNode* const'
这种事情的适当语法是什么?
干杯,
里斯
I'm writing a member function that uses a member variable pointer as an iterator. However I want to reference the pointer within the function purely for readability's sake. Like so:
/* getNext will return a pos object each time it is called for each node
* in the tree. If all nodes have been returned it will return a Pos
* object (-1, -1).
* TODO: Add a lock boolean to tree structure and assert unlocked for
* push/pop.
*/
Pos BTree::getNext () const
{
BTreeNode*& it = this->getNextIter;
while (it)
{
if (it->visited)
{
/* node has been visited already, visit an unvisited right
* child node, or move up the tree
*/
if ( it->child [BTREE_RIGHT] != NULL
&& !it->child [BTREE_RIGHT]->visited)
{
it = it->child [BTREE_RIGHT];
}
else
{
it = it->parent;
}
}
else
{
/* if unvisited nodes exist on the left branch, iterate
* to the smallest (leftmost) of them.
*/
if ( it->child [BTREE_LEFT] != NULL
&& !it->child [BTREE_LEFT]->visited)
{
for (;
it->child [BTREE_LEFT] != NULL;
it = it->child [BTREE_LEFT]) {}
}
else
{
it->visited = 1;
return it->pos;
}
}
}
it = this->root;
this->setTreeNotVisited (this->root);
return Pos (-1, -1);
}
This is basically what I'm going for, where this->getNextIter is a BTreeNode*. However I get the error:
btree.cpp:238: error: invalid initialization of reference of type
'DataTypes::BTreeNode*&' from expression of type 'DataTypes::BTreeNode* const'
What is the appropriate syntax for this kind of thing?
Cheers,
Rhys
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的成员函数是 const 限定的,因此您无法修改成员变量 getNextIter 。您需要使用 const 引用:
但是,在您的函数中,您修改了
it
,因此您可能需要从成员函数中删除const
限定,或者将getNextIter
成员变量可变
。当您有一个 const 限定的成员函数时,所有非可变成员变量在成员函数内部都是 const 限定的,因此为什么编译器报告当您尝试在
getNext()
内部使用getNextIter
时,它的类型为DataTypes::BTreeNode* const
(请注意const
)。Your member function is
const
-qualified, so you cannot modify the member variablegetNextIter
. You need to use a const reference:However, in your function, you modify
it
, so instead you probably need to remove theconst
-qualification from the member function or make thegetNextIter
member variablemutable
.When you have a member function that is
const
-qualified, all non-mutable
member variables areconst
-qualified inside of the member function, hence why the compiler reports that when you try to usegetNextIter
inside ofgetNext()
, it has a type ofDataTypes::BTreeNode* const
(note theconst
).