以下代码的输出是什么?
What's the output of the following code?
此代码发布于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_。另请参阅:最麻烦的解析
相关文章:
- 为对象分配整数.输出将是什么?
- 以下与指针相关的代码的输出是什么?
- 从 c++ 中的实例化进程获取输出的可靠方法是什么?
- 我的输出结果是 0 英寸C++.可能是什么问题
- 使输出流式处理运算符适用于 boost::variant<std::vector<int>、int、double 的正确方法是什么>
- 以下程序在 c++ 中的输出是什么?
- 任何人都解释下面的代码,它的输出是什么?
- 当我打印字符串时,我不知道它是什么输出
- 假设包含所有必需的头文件,输出将是什么
- 如果我获得Integer和Double的产品,输出的数据类型是什么
- Qimage :: Pixel函数的10位输出特别是指的是什么
- 是什么导致异常输出以及如何修复
- C++程序的输出与预期不同的原因是什么?
- 给定输出的逻辑是什么
- 这段代码的输出是什么,它在构造函数中使用 strcpy?
- 将在 C++ 中输出多维数组内容的 for 循环的结构是什么
- 以下代码的输出是什么(答案因差异编译器而异)
- 在一行C 中,该代码后缀和前缀的输出的逻辑是什么
- 该代码将如何工作以及输出的输出是什么
- 在不同于依赖DLL的文件夹中输出已编译目标的正确方法是什么