经常看一些c++的书有这一句每个c++类都有构造函数,只是很多没写,是被编译器隐藏啦!
我表示对这句话的怀疑,于是有了下面的例子
#include <iostream> #include <windows.h> using namespace std; class CInit { int m_nNumber; public: void SetNumber(int nNumber) { m_nNumber = nNumber; } int GetNumber() { return m_nNumber; } }; int main() { CInit Init; Init.SetNumber(5); cout<<Init.GetNumber()<<endl; return 0; }
看一下对应的汇编代码,很显然没有 CInit::CInit 出现
{ 00401570 push ebp 00401571 mov ebp,esp 00401573 sub esp,44h 00401576 push ebx 00401577 push esi 00401578 push edi 00401579 lea edi,[ebp-44h] 0040157C mov ecx,11h 00401581 mov eax,0CCCCCCCCh 00401586 rep stos dword ptr [edi] 28: CInit Init; 29: Init.SetNumber(5); 00401588 push 5 0040158A lea ecx,[ebp-4] 0040158D call @ILT+665(CInit::SetNumber) (0040129e) 30: cout<<Init.GetNumber()<<endl; 00401592 push offset @ILT+195(std::endl) (004010c8) 00401597 lea ecx,[ebp-4] 0040159A call @ILT+255(CInit::GetNumber) (00401104) 0040159F push eax 004015A0 mov ecx,offset std::cout (0047be90) 004015A5 call @ILT+250(std::basic_ostream<char,std::char_traits<char> >::operator<<) (004010ff) 004015AA mov ecx,eax 004015AC call @ILT+480(std::basic_ostream<char,std::char_traits<char> >::operator<<) (004011e5) 31: return 0;