主要探討對象的構造順序與內存的布局,主要參考來源為《Exceptional C++ Style中文版》 ,下載見 http://www.linuxidc.com/Linux/2013-03/81596.htm
文中第十四條 順序!順序!根據文中的代碼摘錄如下,代碼中添加了一些調試的信息。
#include <map>
struct classcomp {
bool operator() (const __int64& lhs, const __int64& rhs) const
{return lhs<rhs;}
};
std::multimap<__int64,std::string,classcomp> m_Construtor;
class B1{
public:
B1()
{
std::cout<<"Constructor B1 "<<this<<'\n';
m_Construtor.insert(std::pair<__int64,std::string>((__int64)this,"Constructor B1 "));
}
virtual ~B1()
{
std::cout<<"Destructor B1 "<<this<<'\n';
}
};
class V1:public B1{
public:
V1()
{
std::cout<<"Constructor V1 "<<this<<'\n';
m_Construtor.insert(std::pair<__int64,std::string>((__int64)this,"Constructor V1 "));
}
virtual ~V1()
{
std::cout<<"Destructor V1 "<<this<<'\n';
}
};
class D1:virtual public V1{
public:
D1()
{
std::cout<<"Constructor D1 "<<this<<'\n';
m_Construtor.insert(std::pair<__int64,std::string>((__int64)this,"Constructor D1 "));
}
virtual ~D1()
{
std::cout<<"Destructor D1 "<<this<<'\n';
}
};
class B2{
public:
B2()
{
std::cout<<"Constructor B2 "<<this<<'\n';
m_Construtor.insert(std::pair<__int64,std::string>((__int64)this,"Constructor B2 "));
}
virtual ~B2()
{
std::cout<<"Destructor B2 "<<this<<'\n';
}
};
class B3{
public:
B3()
{
std::cout<<"Constructor B3 "<<this<<'\n';
m_Construtor.insert(std::pair<__int64,std::string>((__int64)this,"Constructor B3 "));
}
virtual ~B3()
{
std::cout<<"Destructor B3 "<<this<<'\n';
}
};
class V2:public B1,public B2{
public:
V2()
{
std::cout<<"Constructor V2 "<<this<<'\n';
m_Construtor.insert(std::pair<__int64,std::string>((__int64)this,"Constructor V2 "));
}
virtual ~V2()
{
std::cout<<"Destructor V2 "<<this<<'\n';
}
};
class D2:public B3,virtual public V2{
public:
D2()
{
std::cout<<"Constructor D2 "<<this<<'\n';
m_Construtor.insert(std::pair<__int64,std::string>((__int64)this,"Constructor D2 "));
}
virtual ~D2()
{
std::cout<<"Destructor D2 "<<this<<'\n';
}
};
class M1{
public:
M1()
{
std::cout<<"Constructor M1 "<<this<<'\n';
m_Construtor.insert(std::pair<__int64,std::string>((__int64)this,"Constructor M1 "));
}
virtual ~M1()
{
std::cout<<"Destructor M1 "<<this<<'\n';
}
};
class M2{
public:
M2()
{
std::cout<<"Constructor M2 "<<this<<'\n';
m_Construtor.insert(std::pair<__int64,std::string>((__int64)this,"Constructor M2 "));
}
virtual ~M2()
{
std::cout<<"Destructor M2 "<<this<<'\n';
}
};
class X:public D1,public D2
{
public:
X()
{
std::cout<<"Constructor X "<<this<<'\n';
m_Construtor.insert(std::pair<__int64,std::string>((__int64)this,"Constructor X "));
}
virtual ~X()
{
std::cout<<"Destructor X "<<this<<'\n';
}
private:
M1 _m1;
M2 _m2;
};
int _tmain(int argc, _TCHAR* argv[])
{
//B b;
int i;
X *pX = new X;
std::cout<<"--------------------------------------------"<<'\n';
cout.setf(ios::showbase | ios::uppercase); //設置基指示符輸出和數值中的字母大寫輸出
std::multimap<__int64,std::string,classcomp> ::iterator iter;
for (iter = m_Construtor.begin();iter != m_Construtor.end();++iter)//遍歷
{
cout<<std::hex<<(*iter).first<<" "<<(*iter).second<<endl;
}
std::cout<<"--------------------------------------------"<<'\n';
delete pX;
pX = NULL;
std::cin>>i;
return 0;
}
以上程序的運行結果為:
Constructor B1 005F7F94
Constructor V1 005F7F94
Constructor B1 005F7F98
Constructor B2 005F7F9C
Constructor V2 005F7F98
Constructor D1 005F7F88
Constructor B3 005F7F80
Constructor D2 005F7F80
Constructor M1 005F7F8C
Constructor M2 005F7F90
Constructor X 005F7F80
--------------------------------------------
0X5F7F80 Constructor B3
0X5F7F80 Constructor D2
0X5F7F80 Constructor X
0X5F7F88 Constructor D1
0X5F7F8C Constructor M1
0X5F7F90 Constructor M2
0X5F7F94 Constructor B1
0X5F7F94 Constructor V1
0X5F7F98 Constructor B1
0X5F7F98 Constructor V2
0X5F7F9C Constructor B2
--------------------------------------------
Destructor X 005F7F80
Destructor M2 005F7F90
Destructor M1 005F7F8C
Destructor D2 005F7F80
Destructor B3 005F7F80
Destructor D1 005F7F88
Destructor V2 005F7F98
Destructor B2 005F7F9C
Destructor B1 005F7F98
Destructor V1 005F7F94
Destructor B1 005F7F94
可以看出對象的構造順序和書中提示到的順序一致
1.首先構造虛基類子對象
2.其次構造非虛基類子對象.
3.構造成員本身
4.構造對象本身
以上同等級的構造順序遵循從左到右的申明順序。