天天看點

C++運算符重載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;
}
           
c++

繼續閱讀