拷贝构造函数,运算符的重载,6个缺省的函数,连=

一,拷贝构造函数

在这里插入图片描述
常引用可以引用字面常量普通引用不可以
在这里插入图片描述

整个程序的运行创建了多少个对象?

#include<iostream>
using namespace std;
class Object
{
	int value;
public:
	//Object() { cout << "Object::Object" << this << endl; }
	Object(int x ) :value(x) { cout << "Object::Object Create "<<this << endl; }
	~Object() { cout << "Object::~Object " << this << endl; }
	//拷贝构造函数(参数是当前类型的引用)
	Object(Object& obj):value(obj.value)
	//Object(Object obj) : value(obj.value)去掉引用形成死递归
	{
		cout << "Copy Create "<<this << endl;
	}
	void SetValue(int x) { value = x; }
	int GetValue() const { return value; }
};
Object fun(Object obj)//3拷贝构造
{
//整个程序的运行创建了多少个对象
//
 int val=obj.GetValue();
 Object obja(val);//4
 return obja;
}
int main()
{
	Object objx(0);//1
	Object objy(0);//2
	//obja不能直接给objy赋值
	//返回时构建一个副本临时变量称之为将亡值拷贝构造5
	objy = fun(objx);
	return 0;
}

在这里插入图片描述
因为是栈区所以先进后出,先4后3

尽量的减少对象的创建,不以引用返回

class Object
{
	int value;
public:
	//Object() { cout << "Object::Object" << this << endl; }
	Object(int x ) :value(x) { cout << "Object::Object Create "<<this << endl; }
	~Object() { cout << "Object::~Object " << this << endl; }
	//写两个函数提高程序的通用性
	int& Value() { return value;}
	const int& Value()const { return value; }
	//拷贝构造函数(参数是当前类型的引用)
	Object(Object& obj):value(obj.value)
	//Object(Object obj) : value(obj.value)去掉引用形成死递归
	{
		cout << "Copy Create "<<this << endl;
	}
};
Object fun(const Object& obj)// 
{
	//整个程序的运行创建了多少个对象
	//
	int val = obj.Value();
	Object obja(val);//3
	return obja;
}
int main()
{
	Object objx(0);//1
	Object objy(0);//2
	//obja不能直接给objy赋值将亡值的地址给了eax
	//注意你将亡值对象构建在主函数之中(构建在调用者空间中)
	//返回时构建一个副本临时变量称之为将亡值拷贝构造5
	objy = fun(objx);//4
	return 0;
}

以引用返回(不可取,除非返回的值生存期不受函数影响)

Object & fun(const Object& obj)// 
{
	//整个程序的运行创建了多少个对象
	//
	int val = obj.Value();
	Object obja(val);//3
	return obja;
}//已引用返回返回的是地址将obja的地址存于eax中当函数结束后空间被回收
int main()
{
	Object objx(0);//1
	Object objy(0);//2
	//obja不能直接给objy赋值将亡值的地址给了eax
	//返回时构建一个副本临时变量称之为将亡值拷贝构造5
	objy = fun(objx);//4
	return 0;
}

二,运算符的重载

类型名加括号给实参,调动其构造函数构造一个无名对象
Complex Add(const Complex& c)const//将亡值的地址在主函数空间中
Complex Add(const Complex& c)const将亡值在add的空间中

class Complex
{
private:
	double Real,Image;
public:
	Complex() :Real(0), Image(0) {}
	Complex(double r, double i) :Real(r), Image(i) 
	{
		cout << "Creat: " << this << endl;
	}
	Complex(const Complex& x) :Real(x.Real), Image(x.Image)
	{
		cout << "Copy Create: " << this << endl;
	}
	~Complex() { cout << "~Complex: " << this << endl; }
	//Complex Add(const Complex*const this,const Complex& c)
	Complex Add(const Complex& c)const//将亡值的地址在主函数空间中
	//vs2019也不允许这样的存在
	//Complex & Add(const Complex& c)const//将亡值在add的空间中
	{
		double r = this->Real + c.Real;
		double i = this->Image + c.Image;
		/*Complex tmp(r, i);//这样会多产生一个对象相比较下面
		return tmp;*/
		//类型名加括号给实参,调动其构造函数构造一个无名对象
		return Complex(r, i);//此时的无名对象充当将亡值对象
		//return Complex(this->Real+c.Real,this->Image+c.Image);
	}
	void Printf()
	{
		cout << "("<<Real << " + " << Image<<")" << endl;
	}
};

int main()
{
	Complex c1(1.2, 2.3);
	Complex c2(4.5, 5.6);
	Complex c3;
	c3 = c1.Add(c2);//c3=Add(&c1,c2);
	c3.Printf();
}

用运算符的重载

class Complex
{
private:
	double Real,Image;
public:
	Complex() :Real(0), Image(0) {}
	Complex(double r, double i) :Real(r), Image(i) 
	{
		cout << "Creat: " << this << endl;
	}
	Complex(const Complex& x) :Real(x.Real), Image(x.Image)
	{
		cout << "Copy Create: " << this << endl;
	}
	~Complex() { cout << "~Complex: " << this << endl; }
	//Complex Add(const Complex*const this,const Complex& c)
	//operator告诉我们的编译器+是一个合法的函数名
	Complex operator+(const Complex& c)const//运算符的重载											//Complex Add(const Complex& c)const//将亡值的地址在主函数空间中
	//vs2019也不允许这样的存在
	//Complex & Add(const Complex& c)const//将亡值在add的空间中
	{
		double r = this->Real + c.Real;
		double i = this->Image + c.Image;
		/*Complex tmp(r, i);//这样会多产生一个对象相比较下面
		return tmp;*/
		//类型名加括号给实参,调动其构造函数构造一个无名对象
		return Complex(r, i);//此时的无名对象充当将亡值对象
		//return Complex(this->Real+c.Real,this->Image+c.Image);
	}
	void Printf()
	{
		cout << "("<<Real << " + " << Image<<")" << endl;
	}
};

int main()
{
	Complex c1(1.2, 2.3);
	Complex c2(4.5, 5.6);
	Complex c3;
	//c3 = c1Add(c2);//c3=Add(&c1,c2);
	c3 = c1 + c2;
	//c3=c1.operator+(c2);c3=operator+(&c1,c2);
	c3.Printf();
}

c++的六个缺省函数

class Object
{
 public:
Object(){} 缺省构造函数
Object(){}缺省析构函数
Object(const Object & x){}缺省的拷贝构造函数
Object&operator=(const Object&x){return *this;}缺省的赋值语句
Object * operator&(){return this;}//普通对象的取地址符
const Object*operator&()const{return this;}//常对象的去地址符
}

普通的赋值语句和可以实现连=的赋值语句

class Object
{
private:
	int value;
public:
	//Object() { cout << "Object::Object" << this << endl; }
	Object(int x) :value(x) { cout << "Object::Object Create " << this << endl; }
	~Object() { cout << "Object::~Object " << this << endl; }
	//void operator=(const Object& obj)//=运算符的重载赋值语句
	//{
	//	this->value = obj.value;
	//}//obja=objb=objc//因为void不可以进行连续赋值
	/*obja=objb=objc相当于
	 obja=objb .operator=(objc);
	 obja=operator=(&objb,objc);
	*/
	//y要实现连等
	Object & operator=(const Object& obj)
	{
		if (this != &obj)//防止自己给自己赋值
		{
		this->value = obj.value;
		}
		
		return *this;
	}
	//写两个函数提高程序的通用性
	int& Value() { return value; }
	const int& Value()const { return value; }
	//拷贝构造函数(参数是当前类型的引用)
	Object(Object& obj) :value(obj.value)
		//Object(Object obj) : value(obj.value)去掉引用形成死递归
	{
		cout << "Copy Create " << this << endl;
	}
};

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
[选做]下面是一个数组类CArray的定义。要求: (1)在此基础上增加print()成员函数打印数组, (2)重载“=”、“+”、“-” 运算符使之能对该数组类对象进行赋值、加减运算。 (3)写出主函数对该类进行测试。 class CArray {private: int* p_arr; int size; public: CArray(); //缺省构造函数 CArray(int* p_a,int s); //构造函数 CArray(const CArray &r_other);//复制构造函数 ~CArray();//析构函数 int operator[](int pos) const; //访问数组元素值的下标运算符重载函数 int& operator[](int pos); //设置数组元素值的下标运算符重载函数 CArray &operator=(const CArray &other)//赋值运算符“=”重载函数 CArray operator+(const CArray &other) //加运算符“+”重载函数 CArray operator-(const CArray &other) //减运算符“-”重载函数 void print() const; }; CArray:: CArray() { p_arr=NULL; size=0;} CArray:: CArray(int* p_a,int s) { if(s>0) { size=s; p_arr=new int[size]; for(int i=0;i<size;i++) p_arr[i]=p_a[i]; } Else { p_arr=NULL; size=0; } } CArray::CArray(const CArray &r_other) { size=r_other.size; if(size) { p_arr=new int[size]; for(int i=0;i<size;i++) p_arr[i]=r_other.p_arr[i]; } } CArray::~CArray() { if(p_arr) delete[] p_arr; p_arr=NULL; size=0; } int CArray::operator[](int pos) const { if(pos>=size) return p_arr[size-1]; if(pos<0) return p_arr[0]; return p_arr[pos]; } int& CArray::operator[](int pos) { if(pos>=size) return p_arr[size-1]; if(pos<0) return p_arr[0]; return p_arr[pos]; }
06-03

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值