C++-----一个类中缺省的6种函数(转载)

#include
using namespace std;
 
class Test
{
public:
	Test() {}             //默认构造函数
	Test(const Test &t)   //默认拷贝构造函数
	{
		a=t.a;
		p=t.p;
	}
	Test& operator = (const Test &t) //赋值操作符重载
	{
		if(this!=&t)
		{
			a=t.a;
			p=t.p;
		}
		return *this;
	}
	~Test()           //默认析构函数
	{}
	Test* operator&() //取地址操作符重载
	{
		return this;
	}
//	const Test* operator&(const Test * const this) 
	const Test* operator&() const  //const修饰的取地址操作符重载
	{
		return this;
	}
private:
	int a;
	char *p;
};
int main()
{
	Test t; //构造函数
	const Test te;
	Test t1=t; //默认拷贝构造函数
    Test t2;  
	t2=t;   //赋值操作符重载
	Test *t2=&t;  //取地址操作符重载
 
     //const修饰的取地址操作符重载
	const Test *p=&te;//常对象必须要用常量指针接受  常量指针可以接受常量对象和非常量对象的地址
	return 0;
}

你可能感兴趣的:(面向对象(C++))