C++ 构造函数:const 引用初始化时的垃圾

2022-01-05 00:00:00 reference constructor constants c++

这段代码有什么问题,为什么我得到错误的答案:

what is wrong with this code, why do I get wrong answer:

class X
{
private:
        const int a;
        const int& b;
public:
        X(): a(10) , b(20)
        {
        //      std::cout << "constructor : a " << a << std::endl;
        //      std::cout << "constructor : b " << b << std::endl;
        }

        void display()
        {
            std::cout << "display():a:" << a << std::endl;
            std::cout << "display():b:" << b << std::endl;

        }
};


int
main(void)
{
        X x;
        x.display();
return 0;
}

上面的代码会给我结果

display():a:10
display():b:1104441332

但是如果我删除默认构造函数中注释的 2 行,它会给我正确的结果,即

But If I remove the commented 2 lines inside the default constructor it gives me proper result which is

constructor : a 10
constructor : b 20
display():a:10
display():b:20

请帮忙,谢谢

推荐答案

您正在将 b 初始化为对 临时 的引用.

You are initializing b as a reference to a temporary.

20 被创建并且只存在于构造函数的范围内.

The value 20 is created and exists only for the scope of the constructor.

此后代码的行为非常有趣 - 在我的机器上,我得到的值与您发布的值不同,但基本行为仍然不确定.

The behavior of the code after this is very interesting - on my machine, I get different values from the ones you posted, but the fundamental behavior is still nondeterministic.

这是因为当引用指向的值超出范围时,它开始引用垃圾内存,从而产生不可预测的行为.

This is because when the value to which the reference points falls out of scope, it begins to reference garbage memory instead, giving unpredictable behavior.

参见 const 引用是否会延长一个临时的?;答案 https://stackoverflow.com/a/2784304/383402 链接到 C++ 标准的相关部分,特别是以下文字:

See Does a const reference prolong the life of a temporary?; the answer https://stackoverflow.com/a/2784304/383402 links to the relevant section of the C++ standard, specifically the below text:

A temporary bound to a reference member in a constructor’s ctor-initializer
(12.6.2) persists until the constructor exits.

这就是为什么你总是在构造函数的打印中得到正确的值,而且很少(但有时可能!)之后.当构造函数退出时,引用悬??空,所有赌注都关闭.

This is why you always get the right value in the print within the constructor, and rarely (but possibly sometimes!) after. When the constructor exits, the reference dangles and all bets are off.

相关文章