C++ - 奇怪的代码输出
C++ - Weird code output
这段代码:
class A {
public:
int _a;
A():_a(0) { cout << "A default - ctor, ";}
A(int i):_a(i) { cout << "A int - ctor, ";}
operator int() { cout << "A int - operator, "; return 33;}
virtual ~A() { cout << "A dtor, ";}
};
int main() {
A* a = new A(99);
cout << *a << endl;
return 0;
}
我预计输出是:A int-operator, A int-ctor, 33
但真正的输出是:A int-ctor, A int-operator, 33
首先你调用 "new A(99)"
,它调用 "A(int i)"
,它打印"A int - ctor, "
。
下面的行调用 "cout << *a"
,它调用 "operator int()"
,它打印"A int - operator, "
。
然后该行以 "cout << [result] << endl"
完成,这将打印结果 (33) 和一个换行符。
所以"A int - ctor, "
+ "A int - operator, "
+ "33"
+ "endl"
是"A int - ctor, A int - operator, 33"
.
相关文章:
- 我的代码输出总是得到 0
- 代码输出随机符号,我不确定出了什么问题
- 为什么当我正确执行语法时,我的代码输出为 0
- 为什么这段代码输出这么多数字
- 当我输入任何内容时,我的代码输出无效
- 我在让我的代码输出整个链表时遇到问题.它只是输出我修改过的一些文本文件,而不是整个东西
- 在 netbeans 中将C++代码输出为汇编语言
- 使用向量(STL)时,未定义的行为,请说明理性背后的以下代码输出
- 当我尝试运行此代码输出时"merge.exe has stopped working"?
- 欧拉项目 #14 代码输出未到来
- C++中的代码输出出现问题
- 如何删除此代码输出的结束空格
- 这段代码输出什么笑脸
- Win32 键转换代码输出不需要的按键
- 在 QT 中将代码输出到主窗口
- 我无法让此代码输出平均工资率,任何人都可以解释原因
- C++字母等效数字代码输出程序
- 我的代码输出重复
- c++代码输出说明
- C++代码输出负值