【问题标题】:Is this an issue with pointers?这是指针的问题吗?
【发布时间】:2013-02-04 19:08:05
【问题描述】:

尝试自学 C++(我通常使用 Python)并编写了这段代码。

#include <iostream>
using namespace std;


class TreeNode {
    public:
        TreeNode *left;
        TreeNode *right;
        int value;

        TreeNode(int _value);
        void add_node(TreeNode node);
};

TreeNode::TreeNode(int _value) {
    left = 0;
    right = 0;
    value = _value;
    cout << "Creating node with value: " << value << endl;
}

void TreeNode::add_node(TreeNode node) {
    cout << "Adding " << node.value << " to " << value << endl;
    if (node.value < value) {
        cout << node.value << " < " << value << endl;
        if (left) {
            cout << "Left node of " << value << " exists " << left->value << endl;
            left->add_node(node);
        } else {
            cout << "Left node of " << value << " does not exist" << endl;
            left = &node;
        }
    }
    if (node.value > value) {
        cout << node.value << " > " << value << endl;
        if (right) {
            cout << "Right node of " << value << " exists " << right->value << endl;
            right->add_node(node);
        } else {
            cout << "Right node of " << value << " does not exist" << endl;
            right = &node;
        }
    }
}

int main ()
{
    TreeNode root(25);
    TreeNode n1(15);
    TreeNode n2(30);
    TreeNode n3(20);

    root.add_node(n1);
    root.add_node(n2);
    root.add_node(n3);

    cout << root.left->value << endl;
    cout << root.right->value << endl;

    return 0;
}

程序编译但运行结果我不理解。

Creating node with value: 25
Creating node with value: 15
Creating node with value: 30
Creating node with value: 20
Adding 15 to 25
15 < 25
Left node of 25 does not exist
Adding 30 to 25
30 > 25
Right node of 25 does not exist
Adding 20 to 25
20 < 25
Left node of 25 exists 20
Adding 20 to 20
20
20

我原以为最后一点会有所不同。

Adding 20 to 25
20 < 25
Left node of 25 exists 15
Adding 20 to 15
20 > 15
Right node of 15 does not exist
15
30

有人能解释一下这里发生了什么吗?

【问题讨论】:

  • 如果您还不了解指针,最好先学习 C,C++ 有一些曲折会让您作为初学者感到困惑。
  • 更好的是,完全避免使用原始指针并使用现代 C++ 习惯用法。

标签: c++ pointers


【解决方案1】:

您正在复制 TreeNode 副本的地址,而不是 main 中 TreeNode 的地址。
记下注释掉的函数调用,并阅读以下内容: How to pass objects to functions in C++?

//Output with TreeNode node as arg
//Creating node with value: 25
//Creating node with value: 15
//Creating node with value: 30
//Creating node with value: 20
//Adding 15 to 25
//15 < 25
//Left node of 25 does not exist
//Adding 30 to 25
//30 > 25
//Right node of 25 does not exist
//Adding 20 to 25
//20 < 25
//Left node of 25 exists 20
//Adding 20 to 20
//20
//20

//Output with TreeNode & node as arg
//Creating node with value: 25
//Creating node with value: 15
//Creating node with value: 30
//Creating node with value: 20
//Adding 15 to 25
//15 < 25
//Left node of 25 does not exist
//Adding 30 to 25
//30 > 25
//Right node of 25 does not exist
//Adding 20 to 25
//20 < 25
//Left node of 25 exists 15
//Adding 20 to 15
//20 > 15
//Right node of 15 does not exist
//15
//30

#include <iostream>
using namespace std;


class TreeNode {
    public:
        TreeNode *left;
        TreeNode *right;
        int value;

        TreeNode(int _value);
        //void add_node(TreeNode  node);
        void add_node(TreeNode & node);
};

TreeNode::TreeNode(int _value) {
    left = 0;
    right = 0;
    value = _value;
    cout << "Creating node with value: " << value << endl;
}

//void TreeNode::add_node(TreeNode node) {
void TreeNode::add_node(TreeNode & node) {
    cout << "Adding " << node.value << " to " << value << endl;
    if (node.value < value) {
        cout << node.value << " < " << value << endl;
        if (left) {
            cout << "Left node of " << value << " exists " << left->value << endl;
            left->add_node(node);
        } else {
            cout << "Left node of " << value << " does not exist" << endl;
            left = &node;
        }
    }
    if (node.value > value) {
        cout << node.value << " > " << value << endl;
        if (right) {
            cout << "Right node of " << value << " exists " << right->value << endl;
            right->add_node(node);
        } else {
            cout << "Right node of " << value << " does not exist" << endl;
            right = &node;
        }
    }
}

int main ()
{
    TreeNode root(25);
    TreeNode n1(15);
    TreeNode n2(30);
    TreeNode n3(20);

    root.add_node(n1);
    root.add_node(n2);
    root.add_node(n3);

    cout << root.left->value << endl;
    cout << root.right->value << endl;

    return 0;
}

【讨论】:

  • 虽然这可行,但值得注意的是,在实际软件中,我们通常不会像这样构建树。我们使用动态分配并传递指向节点的指针
  • 真的吗?我通常添加 元素 并让树在内部处理节点的分配和放置。
  • @n.m.如果数据结构包含常量数据,我会考虑这样构造。
  • @DavidD 谢谢。第一句话确实说明了一切,但这个例子也很有帮助。
  • @n.m 和 paddy 我考虑了你提到的两种方法,但正如你可能怀疑的那样,这不是“真正的软件”,它只是我为了学习而搞砸的。我会根据你提到的条款做一些阅读。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-06-07
  • 2011-08-18
  • 1970-01-01
  • 1970-01-01
  • 2017-04-17
  • 2018-07-28
  • 1970-01-01
相关资源
最近更新 更多