#include <iostream> using namespace std; class CTest { public: CTest(void) { cout << "ct of CTest called" << endl; } }; int main() { CTest t1; CTest t2(); return 0; }
代码如上,一直认为 line 16 也会调用默认构造函数,最近发现不是自己想的那样。
当调试运行的时候,反汇编模式下 line 16 被直接跳过去了。一开始以为是编译器优化了,但是想错了。其实这个时候仔细观察编译器的输出,是有提示的,vs2010提示如下
Debug Win32 ------
1> main.cpp
1>main.cpp(16): warning C4930: 'CTest t2(void)': prototyped function not called (was a variable definition intended?)
1> Constructor.vcxproj -> Constructor.exe
这句话与函数有关,看来编译器是把 line 16作为一个函数声明的。