通过参考(c++)传递对象

发布于 2025-02-11 06:49:23 字数 1552 浏览 2 评论 0原文

我想要一种从数组中创建二进制树而没有返回的方法。因此,我必须通过参考来工作,但是我在适当的语法中遇到了一些麻烦。

显然,我已经尝试在发布此问题之前尝试搜索,我已经看到了类似的帖子,但是没有答案确实有帮助。

到目前为止,我在这里所做的事情:

我的班级:

class Node
{
private:
    int data;
    Node* left;
    Node* right;
public:

    [...]

    void make_tree(Node* node, int values[], int size);
};

方法:

// Every operations are done a copy of node, I want them to be done on the original object
void Node::make_tree(Node* node, int values[], int size)
{
    if (size <= 0)
    {
        return;
    }
    else
    {

        if (!node)
        {
            node = new Node(values[size]);
        }
        else if (!node->has_data())
        {
            node->set_data(values[size]);
        }
        // recursive call
        make_tree(node->left, values, size - 1);
        make_tree(node->right, values, size - 1);

    }
}

呼叫:

int main()
{
    int tab[] = { 5,3,4,9,7,6 };
    Node* root = new Node();

    /* I want "root" to be passed by reference so every modifications
     are done on the original object and not a copy who will be destroyed
     at the end of the method scope. */

    root->make_tree(root, tab, 6);

    root->print_tree();   // there is nothing more in root
}

由于我已经将指针传递给对象“根”,所以我对如何做到这一点感到困惑。

谢谢。

PS:我知道我的递归电话没有做我所描述的事情。这是其他时间的问题。

pps:第一个帖子,因此,如果您看到我做错了什么,请告诉我。

I want a method that creates a binary tree from an array and returns nothing. So I would have to work by reference but I am having some troubles with the proper syntax to use.

I have obviously tried to search before posting this question, I have seen similar posts but no answer were truly helpfull.

Here what i have done so far :

My Class :

class Node
{
private:
    int data;
    Node* left;
    Node* right;
public:

    [...]

    void make_tree(Node* node, int values[], int size);
};

The method :

// Every operations are done a copy of node, I want them to be done on the original object
void Node::make_tree(Node* node, int values[], int size)
{
    if (size <= 0)
    {
        return;
    }
    else
    {

        if (!node)
        {
            node = new Node(values[size]);
        }
        else if (!node->has_data())
        {
            node->set_data(values[size]);
        }
        // recursive call
        make_tree(node->left, values, size - 1);
        make_tree(node->right, values, size - 1);

    }
}

The call :

int main()
{
    int tab[] = { 5,3,4,9,7,6 };
    Node* root = new Node();

    /* I want "root" to be passed by reference so every modifications
     are done on the original object and not a copy who will be destroyed
     at the end of the method scope. */

    root->make_tree(root, tab, 6);

    root->print_tree();   // there is nothing more in root
}

Since I am already passing a pointer to the object "root", I am confused on how I could do it.

Thank you.

PS: I am aware that my recursive call does not do what I described it should do. That is a problem for an other time.

PPS : first post btw, so if you see something I did wrong, please, tell me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

庆幸我还是我 2025-02-18 06:49:23
void Node::make_tree(Node* node, int values[], int size);

节点指针在函数中无法在函数中进行修改,因为您按值传递(仅修改了副本)。

您可以使用注释中建议的参考:

 void Node::make_tree(Node* &node, int values[], int size);

或者还可以使用指针指向指针void node :: make_tree(node ** node ** node,int values [],int size);修改您的代码将是更多的工作。

void Node::make_tree(Node* node, int values[], int size);

Node pointer cannot be modified inside the function, as you pass it by value (only the copy is modified).

You can use a reference, as suggested in comments :

 void Node::make_tree(Node* &node, int values[], int size);

Or you can also use a pointer to a pointer void Node::make_tree(Node** node, int values[], int size); but there will be more work to modify your code.

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