在 C++ 中引用指针成员函数

发布于 2024-09-17 22:53:15 字数 1866 浏览 4 评论 0原文

我正在编写一个使用成员变量指针作为迭代器的成员函数。不过,我想纯粹为了可读性而引用函数内的指针。就像这样:

/* 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦在深巷 2024-09-24 22:53:15

您的成员函数是 const 限定的,因此您无法修改成员变量 getNextIter 。您需要使用 const 引用:

BTreeNode * const & it = getNextIter;

但是,在您的函数中,您修改了 it,因此您可能需要从成员函数中删除 const 限定,或者将getNextIter 成员变量可变

当您有一个 const 限定的成员函数时,所有非可变成员变量在成员函数内部都是 const 限定的,因此为什么编译器报告当您尝试在 getNext() 内部使用 getNextIter 时,它的类型为 DataTypes::BTreeNode* const (请注意 const)。

Your member function is const-qualified, so you cannot modify the member variable getNextIter. You need to use a const reference:

BTreeNode * const & it = getNextIter;

However, in your function, you modify it, so instead you probably need to remove the const-qualification from the member function or make the getNextIter member variable mutable.

When you have a member function that is const-qualified, all non-mutable member variables are const-qualified inside of the member function, hence why the compiler reports that when you try to use getNextIter inside of getNext(), it has a type of DataTypes::BTreeNode* const (note the const).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文