系统中默认的六种函数(构造 拷贝构造 赋值 析构 ..........)

#include<iostream>
using namespace std;

//系统默认的六种函数。除此之外系统对其他函数时没有默认的 需要作者自己编译
class Test 
{
public:
	//1-构造函数
	Test (int d=0):data(d)
	{
		cout<<"create Test"<<endl;
	}
	//2-拷贝构造
	Test (const Test &t)
	{
		data=t.data;
	}
	//3-赋值语句
	Test& operator=(const Test &t)
	{
		if(this !=&t)
		{
			data=t.data;
		}
		return *this;
	}
	//4-析构函数
	~Test()
	{
		cout<<"free Test"<<endl;
	}
public:
	//5-一般对象的取地址符重载
	Test* operator&()
	{
		return this;
	}
	//6-常对象的取址运算符的重载。
	const Test* operator&()const
	{
		return this;
	}
private:
	int data;
};
void main()
{
	Test t;
	Test t1=t;
	Test t2;
	t2=t1;
	int a=10;
	&a;
	double d=12.34;
	&d

	Test t3;
	Test *pt=&t3;//Test *pt=t3.operator&()
}

构造 拷贝构造 赋值 析构 一般对象取地址符的重载 常对象取址运算符的重载 这六种函数在计算机的系统中是默认存在的。即使在编译的时候不写,计算机也会编译通过。但其他的函数不可以。

猜你喜欢

转载自blog.csdn.net/weixin_41747893/article/details/87993297
今日推荐