复制构造函数--赋值构造函数 举例
时间:2010-06-13 来源:wwm
#include <iostream>
using namespace std;
class ABC{
private:
int m_i;
void * m_p;
public:
ABC(){};
ABC(void*p):m_p(p){cout<<"构造ABC2="<<p<<endl;};
};
class Test
{
public:
Test();
~Test();
Test(const Test& t);
Test& operator=(const Test& t);
private:
int t1;
public:
ABC m_abc;
};
Test::Test():m_abc(this)
{
cout<<"调用构造函数"<<this<<endl;
}
Test::~Test()
{
cout<<"调用析构函数"<<endl;
}
Test::Test(const Test& t)
{
cout<<"调用复制构造函数"<<endl;
}
Test& Test::operator =(const Test& t)
{
cout<<"调用赋值构造函数"<<endl;
t1 = t.t1;
return *this;
}
int main()
{
Test t1;
Test t2 = t1;
//Test t3;
//t3 = t1;
return 0;
}
using namespace std;
class ABC{
private:
int m_i;
void * m_p;
public:
ABC(){};
ABC(void*p):m_p(p){cout<<"构造ABC2="<<p<<endl;};
};
class Test
{
public:
Test();
~Test();
Test(const Test& t);
Test& operator=(const Test& t);
private:
int t1;
public:
ABC m_abc;
};
Test::Test():m_abc(this)
{
cout<<"调用构造函数"<<this<<endl;
}
Test::~Test()
{
cout<<"调用析构函数"<<endl;
}
Test::Test(const Test& t)
{
cout<<"调用复制构造函数"<<endl;
}
Test& Test::operator =(const Test& t)
{
cout<<"调用赋值构造函数"<<endl;
t1 = t.t1;
return *this;
}
int main()
{
Test t1;
Test t2 = t1;
//Test t3;
//t3 = t1;
return 0;
}
相关阅读 更多 +