简体   繁体   English

std::cout 的奇怪行为

[英]Weird Behavior of std::cout

I have a class for representing nodes in C++ but when printing the output out to with std::cout I am seeing a weird issue.我有一个用 C++ 表示节点的类,但是当用std::cout打印输出时,我看到了一个奇怪的问题。 Here is the class:这是课程:

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

        NodeInt()
        {
            left    = NULL;
            right   = NULL;
        };

        NodeInt(int val)
        {
            value = val;
            left    = NULL;
            right   = NULL;
        };

        void setValue(int val)
        {
            value = val;
        };

        void insertNode(int val)
        {
            if(val <= value )
            {
                if(left == NULL)
                {
                    NodeInt newNode;
                    newNode.setValue(val);
                    left = &newNode;
                }
                else
                {
                    left->insertNode(val);
                }
            }
            else
            {
                if(right == NULL)
                {
                    NodeInt newNode;
                    newNode.setValue(val);
                    right = &newNode;
                }
                else
                {
                    right->insertNode(val);
                }
            }
        };
};

Here is the main:这里是主要的:

int main()
{
    NodeInt firstN;
    firstN.setValue(27);
    firstN.insertNode(11);
    firstN.insertNode(29);
    firstN.insertNode(10);

    /**std::cout << firstN.value << std::endl;
    std::cout << firstN.left->value << std::endl;
    std::cout << firstN.right->value << std::endl;
    **/

    NodeInt* n = firstN.left;
    n = (n->left);
    std::cout << n->value << std::endl;

    return 0;
}

Now you'll see I have three std::cout lines commented out.现在你会看到我注释掉了三个 std::cout 行。 As it is if I run the program right now it will output the correct value of 10. However, if I un-comment the lines it will change the 10 to 2130567168. Picture is showing what I mean:就像我现在运行程序一样,它会输出正确的值 10。但是,如果我取消注释这些行,它会将 10 更改为 2130567168。图片显示了我的意思:

在此处输入图片说明

What gives?是什么赋予了?

You set the two pointers of your Node to point to function-local variables in insertNode() that will be gone when execution reaches the end of the block they are defined in. The output you get is pure luck.您将Node的两个指针设置为指向insertNode()中的函数局部变量,当执行到达定义它们的块的末尾时,这些变量将消失。您得到的输出纯属运气。

If you want to do manual memory management you'll have to use new and delete and your class needs a copy-constructor, a copy-assignment-operator and a destructor.如果您想进行手动内存管理,您将不得不使用newdelete并且您的类需要一个复制构造函数、一个复制赋值运算符和一个析构函数。 Please read The Rule of the Big Three (and a half) and The Rule of the Big Four (and a half) (aka The Rule of 3/5).请阅读三巨头(半) 的规则和四大(半)的规则(又名 3/5 规则)。

If you want to have an easier life however you can look up smart-pointers and how to use them and follow The Rule of Zero .但是,如果您想拥有更轻松的生活,则可以查找智能指针以及如何使用它们并遵循零规则

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM