普通构造函数函数体内初始化:(事实上都是先初始化阶段再计算阶段)
#include <iostream>
using namespace std;
class A
{
public:
A() // 无参构造函数
{
cout << "Construct A" << endl ;
}
A(const A& ObjA) // 拷贝构造函数
{
cout << "Copy constructor for A" << endl ;this->a = ObjA.a;
}
A& operator = (const A& ObjA) // 赋值运算符,用来赋值而非初始化
{
cout << "assignment for A" << endl ;this->a = ObjA.a ;return *this;
}
int a ;
};
struct B
{
<span style="white-space:pre"> </span>A A1; <span style="white-space:pre"> </span>// Test2有一个Test1类的内嵌对象
<span style="white-space:pre"> </span>B(A &ObjA)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>A1 = ObjA ;
<span style="white-space:pre"> </s