1. 程式人生 > 實用技巧 >C++——運算子過載(上)

C++——運算子過載(上)

運算子過載

運算子過載概念:對已有的運算子重新進行定義,賦予其另一種功能,以適應不同的資料型別

e.g:

對於內建資料型別,編譯器知道如何進行運算

int a = 10;
int b = 10;
int c = a + b;

對於一些自定義的資料型別,編譯器不知道該如何進行運算,所有需要我們進行運算子過載

class Person
{
public:
    int m_A;
    int m_B;
}

Person p1;
p1.m_A = 10;
p1.m_B = 10;

Person p2;
p2.m_A = 10;
p2.m_B = 10;

//編譯器不知道該如何進行運算,所以需要我們自己進行運算子過載
Person p3 = p1 + p2;

通過自己寫的成員函式,可以實現兩個物件相加屬性後返回新的物件

Person PersonAddPerson(Person &p)
{
    Person temp;
    temp.m_A = this->m_A + p.m_A;
    temp.m_B = this->m_B + p.m_B;
    return temp;
}

對於編譯器所給的加法運算通用名稱進行過載

1.通過成員函式過載+符號

Person operator+ (Person &p)
{
    Person temp;
    temp.m_A = this->m_A + p.m_A;
    temp.m_B = this->m_B + p.m_B;
    return temp;
}
Person p3 = p1.operator+(p2);//實際操作
//簡化為
Person p3 = p1 + p2;

2.通過全域性函式過載+符號

Person operator+ (Person &p1,Person &p2)
{
    Person temp;
    temp.m_A = p1.m_A + p2.m_A;
    temp.m_B = p1.m_B + p2.m_B;
    return temp;
}
Person p3 = operator+(p1,p2);//實際操作
//簡化為
Person p3 = p1 + p2;

1 加號運算子過載

作用:實現兩個自定義資料型別相加的運算

#include <iostream>
using namespace std;

//加號運算子過載

class Person
{
//public:
//	//1.通過成員函式對加法運算子進行過載
//	Person operator+(Person &p)
//	{
//		Person temp;
//		temp.m_A = this->m_A + p.m_A;
//		temp.m_B = this->m_B + p.m_B;
//		return temp;
//	}

public:

	int m_A;
	int m_B;
};
//2.通過全域性函式對加法運算子進行過載
Person operator+(Person &p1, Person &p2)
{
	Person temp;
	temp.m_A = p1.m_A + p2.m_A;
	temp.m_B = p1.m_B + p2.m_B;
	return temp;
}
//運算子過載也可以發生函式過載
Person operator+(Person &p1, int val)
{
	Person temp;
	temp.m_A = p1.m_A + val;
	temp.m_B = p1.m_B + val;
	return temp;
}

void test01()
{
	Person p1;
	p1.m_A = 10;
	p1.m_B = 10;
	Person p2;
	p2.m_A = 20;
	p2.m_B = 20;

	Person p3 = p1 + p2;
	//成員函式過載的本質呼叫
	//Person p3=p1.operator+(p2);
	//全域性函式過載的本質呼叫
	//Person p3 = operator+(p1, p2);

	cout << "p3.m_A = " << p3.m_A << endl;
	cout << "p3.m_B = " << p3.m_B << endl;

	//呼叫過載的函式
	Person p4 = p1 + 10;//Person + int
	cout << "p4.m_A = " << p4.m_A << endl;
	cout << "p4.m_B = " << p4.m_B << endl;

}


int main()
{
	test01();

	system("pause");

	return 0;
}

總結1:對於內建的資料型別的表示式的的運算子是不可能改變的

總結2:不要濫用運算子過載

2 左移運算子過載

作用:可以輸出自定義資料型別

class Person {
	friend ostream& operator<<(ostream& out, Person& p);

public:

	Person(int a, int b)
	{
		this->m_A = a;
		this->m_B = b;
	}

	//成員函式 實現不了  p << cout 不是我們想要的效果
	//void operator<<(Person& p){
	//}

private:
	int m_A;
	int m_B;
};

//全域性函式實現左移過載
//ostream物件只能有一個
ostream& operator<<(ostream& out, Person& p) {
	out << "a:" << p.m_A << " b:" << p.m_B;
	return out;
}

void test() {

	Person p1(10, 20);

	cout << p1 << "hello world" << endl; //鏈式程式設計
}

int main() {

	test();

	system("pause");

	return 0;
}

總結:過載左移運算子配合友元可以實現輸出自定義資料型別

3 遞增運算子過載

作用: 通過過載遞增運算子,實現自己的整型資料

#include <iostream>
using namespace std;

//過載遞增運算子
class MyInteger
{
	//友元宣告
	friend ostream & operator<<(ostream &cout, MyInteger &Myint);
    
public:
	MyInteger()
	{
		m_Num = 0;
	}
	//過載前置++運算子,返回引用型別是為了一直對一個數據進行遞增
	MyInteger & operator++()
	{
		//先進行++操作
		m_Num++;
		//返回自身
		return *this;
	}
	//過載後置++運算子,
	//void operator++(int)  int 是一個佔位引數,可以用來區分前置和後置
	//後置遞增必須要返回值,不能返回引用,如果返回引用就等於返回一個區域性變數的引用
	MyInteger operator++(int)
	{
		//先記錄當前的資料
		MyInteger temp = *this;

		//再進行++操作
		this->m_Num++;

		//再返回記錄的資料值
		return temp;
	}
    
private:
	int m_Num;
 };

//過載左移運算子
ostream & operator<<(ostream &cout, MyInteger &Myint)
{
	cout << Myint.m_Num;
	return cout;
}
//對前置遞增運算子進行測試
void test01()
{
	MyInteger Myint;
	//cout << Myint << endl;
	cout << ++(++Myint) << endl;
	cout << Myint << endl;
}

//對後置遞增運算子進行測試
void test02()
{
	MyInteger myint;

	cout << myint++ << endl;

	cout << myint << endl;

}
int main()
{
	test01();

	test02();

	system("pause");

	return 0;
}

總結: 前置遞增返回引用,後置遞增返回值

自己練習遞減運算子的過載

#include <iostream>
using namespace std;

//遞減運算子過載
class MyInteger
{
	friend ostream & operator<<(ostream & cout, MyInteger myint);

public:
	MyInteger()
	{
		this->m_Num = 10;
	}
	
	//過載前置左移運算子
	MyInteger & operator--()
	{
		//先進行--操作
		this->m_Num--;
		//返回物件本身
		return *this;
	}

	//過載後置左移運算子
	MyInteger operator--(int)
	{
		//先記錄物件當前值
		MyInteger temp = *this;
		//進行--操作
		this->m_Num--;
		//再返回記錄的值
		return temp;
	}

private:
	int m_Num;
};
//過載左移運算子
ostream & operator<<(ostream & cout, MyInteger myint)
{
	cout << myint.m_Num;
	return cout;
}

void test01()
{
	MyInteger myint;
	cout << myint << endl;
	cout << --myint << endl;
	cout << myint << endl;
}

void test02()
{
	MyInteger myint;
	cout << myint << endl;
	cout << myint-- << endl;
	cout << myint << endl;
}

int main()
{
	//test01();

	test02();

	system("pause");

	return 0;
}