摘要:
class CExample
{
private:
int a;
public:
//构造函数
CExample()
{
a = 0;
}
//构造函数
CExample(int b)
{
a = b;
std::cout<<"creat: "<<a<<std::endl;
}
//拷贝构造
CExample(const CExample& C)
{
a = C.a;
std::cout<<"copy"<<std::endl;
}
//析构函数
~CExample()
{
std::cout<< "delete: "<<a<<std::endl;
}
//赋值运算符重载
CExample& operator = (const CExample& C)
{
a = C.a;
std::cout<<"operator ="<<std::endl;
return *this;
}
void Show ()
{
std::cout<<a<<std::endl;
}
};
CExample[R] g_Fun(CExample C)//<- 方式1:参数-值传递</span>
{
CExample T(0);
return T; //<- 方式2:返回-值传递</span>
}
void main()
{
//构造创建S->拷贝构造以S创建C->构造创建T->拷贝构造以T创建R[->析构T->C->R->S]
CExample S(1);
CExample R = g_Fun(S); //同:/*CExample R = */g_Fun(S);
//拷贝构造以S创建S2 //<- 方式3:直接调用</span>
CExample S2 = S; //同:CExample S2(S);
//构造创建S3->以S调用赋值运算重载
CExample S3;
S3 = S;
}