以下代码的输出是什么?

What's the output of the following code?

本文关键字:是什么 输出 代码      更新时间:2023-10-16

此代码发布于http://accu.org/index.php/cvujournal,2013年7月发行。我无法理解输出,任何解释都有助于

#include <iostream>
int x;
struct i
{
    i() { 
        x = 0;
        std::cout << "--C1n";
    }
    i(int i) {
        x = i;
        std::cout << "--C2n";
    }
};
class l
{
public:
    l(int i) : x(i) {}
    void load() {
        i(x);
    }
private:
    int x;
};
int main()
{
    l l(42);
    l.load();
    std::cout << x << std::endl;
}

输出:

--C1
0

我期待着:

--C2
42

有什么解释吗?

i(x);等价于i x;,抛出一对多余的圆括号。它声明了一个名为x的变量,类型为i,默认初始化;它不创建以CCD_ 6作为构造函数参数的CCD_。另请参阅:最麻烦的解析