根据下面的代码你可以更好的理解这6个函数,要看概念可以参考我的前面几篇博客
class Test
{
public:
Test(int a = 0)
{
cout << "构造函数" << this << endl;
date = a;
}
Test(const Test&t)
{
cout << "拷贝构造" << this << endl;
date = t.date;
}
Test&operator=(const Test&t)
{
cout << "赋值函数" << this << endl;
if (this != &t)
{
date = t.date;
}
return *this;
}
int getdate()
{
return date;
}
/*
Test* operator&()
{
cout << "取地址符重载" << this << endl;
return this;
}
const Test* operator&()const
{
cout << "const修饰的取地址符重载" << this << endl;
return this;
}
*/
private:
int date;
};
int main()
{
Test t1;
int i = 0;
cout << "t.date=" << t1.getdate() << endl;
t1 = Test(100);//调用构造函数转化为类类型,然后再调用赋值
Test t2 = t1;//调用拷贝构造
Test t3;
t3 = t2 = t1;//调用两次赋值函数
cout << "t3.date=" << t3.getdate() << endl;
/*
Test t4(200);
const Test t5 = t4;
Test *p = &t4;
const Test *pt = &t5;
cout << p->getdate() << endl;
*/
return 0;
}