C++运算符重载

C++运算符重载

更详细讲解可阅读 https://www.jianshu.com/p/b7c2e25cb4b6

加号运算符(+)重载

参考 https://www.bilibili.com/video/av41559729?p=121
方法1. 成员函数重载

#include <iostream>
using namespace std;

class Obj
{
public:
	Obj operator+(Obj &o)
	{
		Obj temp;
		temp.value = this->value + o.value;
		return temp;
	}
	int value;
};

int main()
{
	Obj o1;
	Obj o2;
	o1.value = 10;
	o2.value = 20;

	Obj o3 = o1 + o2;  // 等价于 Obj o3 = o1.operator+(o2);
	cout << o3.value << endl;
	return 0;
}

方法2. 全局函数重载

#include <iostream>
using namespace std;

class Obj
{
public:
	int value;
};

Obj operator+(Obj &o1, Obj &o2)
{
	Obj temp;
	temp.value = o1.value + o2.value;
	return temp;
}

int main()
{
	Obj o1;
	Obj o2;
	o1.value = 10;
	o2.value = 20;

	Obj o3 = o1 + o2;  // 等价于 Obj o3 = operator+(o1, o2);
	cout << o3.value << endl;
	return 0;
}

左移运算符(<<)重载

参考 https://www.bilibili.com/video/av41559729?p=122

#include <iostream>
using namespace std;

class Obj
{
public:
	int value1;
	int value2;
};

// out为cout的引用
ostream & operator<<(ostream &out, Obj &o)
{
	out << o.value1 << " " << o.value2;
	return out;
}

int main()
{
	Obj o;
	o.value1 = 10;
	o.value2 = 20;
	cout << o << " blabalbla..." << endl;  //打印结果 10 20 blabalbla...
	return 0;
}

配合友元可以更好的使用

#include <iostream>
using namespace std;

class Obj
{
	friend ostream & operator<<(ostream &out, Obj &o);
public:
	Obj(int v1, int v2)
	{
		this->value1 = v1;
		this->value2 = v2;
	}
private:
	int value1;
	int value2;
};

// out为cout的引用
ostream & operator<<(ostream &out, Obj &o)
{
	out << o.value1 << " " << o.value2;
	return out;
}
	

int main()
{
	Obj o(10, 20);
	cout << o << " blabalbla..." << endl; // 打印结果 10 20 blabalbla...
	return 0;
}

递增运算符(++)重载

int a = 0;
++a //前置递增
a++ //后置递增
++(++a) //写法正确,前置运算返回的是对象的引用
(a++)++ //错误,无法运行

为了与内置版本保持一致,前置运算符应该返回递增或递减后对象的引用,后置运算符应该返回对象的原值(递增和递减之前),返回的形式是值而非引用。
https://www.jianshu.com/p/b7c2e25cb4b6

#include <iostream>
using namespace std;

class Obj
{

public:
	Obj(int v)
	{
		this->value = v;
	}
	
	// 前置递增
	Obj& operator++()
	{
		this->value ++;
		return *this;
	}
	
	// 后置递增, int只是为了区分前置和后置运算符
	Obj operator++(int)
	{
		Obj temp = *this;
		this->value ++;
		return temp;
	}
	
	int getValue()
	{
		return this->value;
	}
	
private:
	int value;
};

int main()
{
	Obj o1(0);
	cout << (++o1).getValue() << endl; // 打印 1
	
	Obj o2(0);
	cout << (o2++).getValue() << endl; // 打印 0
	return 0;
}

赋值运算符(=)重载

主要是为了解决浅拷贝的不足

#include <iostream>
using namespace std;

class Obj
{

public:
	Obj(int v)
	{
		this->pValue = new int(v);
	}
	
	// 前置递增
	Obj& operator=(Obj& o)
	{
		if(this->pValue != NULL)
		{
			delete this->pValue;
			this->pValue = NULL;
		}
		this->pValue = new int(*o.pValue);
		return *this;
	}
	
	int getValue()
	{
		return *this->pValue;
	}
	
	void setValue(int v)
	{
		*this->pValue = v;
	}
	
private:
	int * pValue;
};

int main()
{
	Obj o1(10);
	Obj o2(20);
	Obj o3(30);
	o3 = o2 = o1;
	cout << o1.getValue() << " " << o2.getValue() << " " << o3.getValue() << endl; // 打印 10 10 10
	o1.setValue(15);
	cout << o1.getValue() << " " << o2.getValue() << " " << o3.getValue() << endl; // 打印 15 10 10
	return 0;
}

关系运算符(==、>、<)重载

#include <iostream>
using namespace std;

class Obj
{

public:
	Obj(int v)
	{
		this->value = v;
	}
	
	// ==  大于小于同理
	bool operator==(Obj& o)
	{
		return this->value == o.value;
	}
	
	int getValue()
	{
		return this->value;
	}
	
private:
	int value;
};

int main()
{
	Obj o1(10);
	Obj o2(10);
	cout << (o1 == o2) << endl; // 打印 1
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值