C++運算子過載

Thrush發表於2021-04-04

加號運算子過載

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

但是對於自己封裝的類,編譯器無法進行運算

這時可以通過自己定義運算子過載進行運算

operator+

通過成員函式過載+號

#include<iostream>
using namespace std;
class Person
{
public:
	int m_a;
	int m_b;
	//通過成員函式實現過載
	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;
	}
};
void test01()
{
	Person p1;
	p1.m_a = 66;
	p1.m_b = 44;
	Person p2;
	p2.m_a = 6;
	p2.m_b = 4;
	Person p3;
	//通過函式原型呼叫
	p3 = p1.operator+(p2);
	//簡便呼叫
	//p3 = p1 + p2;
	cout << "p3.m_a:" << p3.m_a << endl;
	cout << "p3.m_b:" << p3.m_b << endl;
}

int main()
{
	test01();
	system("pause");
	return 0;
}

注意兩種呼叫方式

通過函式原型呼叫
p3 = p1.operator+(p2);
簡便呼叫
p3 = p1 + p2;

通過全域性函式過載+號

#include<iostream>
using namespace std;
class Person
{
public:
	int m_a;
	int m_b;
};
//通過全域性函式實現過載
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;
}
void test01()
{
	Person p1;
	p1.m_a = 66;
	p1.m_b = 44;
	Person p2;
	p2.m_a = 6;
	p2.m_b = 4;
	Person p3;
	//函式原型呼叫
	p3 = operator+(p1,p2);
	//簡便呼叫
	//p3 = p1 + p2;
	cout << "p3.m_a:" << p3.m_a << endl;
	cout << "p3.m_b:" << p3.m_b << endl;
}
int main()
{
	test01();
	system("pause");
	return 0;
}

注意兩種呼叫方式

通過函式原型呼叫
p3 = operator+(p1,p2);
簡便呼叫
p3 = p1 + p2;

運算子過載發生函式過載

運算子過載可以發生函式過載:Person+int等等

#include<iostream>
using namespace std;
class Person
{
public:
	int m_a;
	int m_b;
};
//通過全域性函式實現過載
Person operator+ (Person& p1, int num)
{
	//建立一個臨時變數
	Person temp;
	temp.m_a = p1.m_a + num;
	temp.m_b = p1.m_b + num;
	return temp;
}
void test01()
{
	Person p1;
	p1.m_a = 66;
	p1.m_b = 44;
	Person p2;
	p2.m_a = 6;
	p2.m_b = 4;
	Person p3;
	//函式原型呼叫
	//p3 = operator+(p1,55);
	//簡便呼叫
	p3 = p1 + 55;
	cout << "p3.m_a:" << p3.m_a << endl;
	cout << "p3.m_b:" << p3.m_b << endl;
}
int main()
{
	test01();
	system("pause");
	return 0;
}

呼叫方法和定義方法與上面相同,不再多餘贅述

總結

1、系統內建資料型別的表示式不可改變

2、不要濫用運算子過載

左移運算子

不利用成員函式過載左移運算子

沒有具體演示,因為報錯,我也沒寫出來

下面通過全域性函式實現

#include<iostream>
using namespace std;
class Person
{
public:
	int m_a;
	int m_b;
	
};
ostream& operator<<(ostream& cout,Person&p)
{
	cout << "p.m_a=" <<p. m_a << "  p.m_b=" <<p. m_b << endl;
	return cout;
}
void test01()
{
	Person p1;
	p1.m_a = 44;
	p1.m_b = 66;
	cout << p1 << endl;
}
int main()
{
	test01();
	system("pause");
	return 0;
}

因為要實現鏈式,實現追加,所以返回值必須是ostream

總結

配合友元實現自定義輸出型別

遞增運算子過載

遞增運算子過載

#include<iostream>
using namespace std;
class myInt
{
	friend ostream& operator<<(ostream& cout, myInt num);
public:
	myInt()
	{
		this->m_a = 0;
	}
	//前置++運算子過載
	myInt& operator++()//返回引用是為了一直對一個資料進行遞增,否則函式預設返回一個新的數
	{
		//先進行++
		m_a++;
		//然後返回自身
		return *this;
	}
	//後置++運算子過載
	myInt operator++(int)//int表示佔位引數,用於區分前置後置引數
	{
		//先記錄當前的值
		myInt temp=*this;
		//再遞增
		m_a++;
		//然後返回記錄的值
		return  temp;
	}
private:
	int m_a;	
};
//左移運算子過載
ostream& operator<<(ostream& cout, myInt num)
{
	cout << num.m_a;
	return cout;
}
void test01()
{
	myInt myint;
	cout << myint << endl;
	cout << ++myint << endl;
	cout << myint << endl;

}
int main()
{
	test01();
	system("pause");
	return 0;
}

賦值運算子過載

#include<iostream>
using namespace std;
class Person
{
public:
	Person(int num)//將資料開闢到堆區
	{
		m_a = new int(num);
	}
	~Person()
	{
		if (m_a != NULL)
		{
			delete m_a;
			m_a = NULL;
		}
	}
	//過載賦值運算子
	Person& operator=(Person &p)//返回值用Person返回本身,可執行連等
	{
		//先判斷是否有屬性在堆區,如果有先釋放乾淨
		if (m_a != NULL)
		{
			delete m_a;
			m_a = NULL;
		}
		m_a = new int(*p.m_a);
		return *this;
	}
	int* m_a;
	
};
void test01()
{
	Person p1(18);
	Person p2(209);
	Person p3(9);
	p2 = p1 = p3;
	cout << *p1.m_a << endl;
	cout << *p2.m_a << endl;
	cout << *p3.m_a << endl;
}
int main()
{
	test01();
	system("pause");
	return 0;
}

關係運算子過載

#include<iostream>
using namespace std;
#include<string>
class Person
{
public:
	
	Person(string name, int age)
	{
		this->age = age;
		this->name = name;
	}
	bool operator==(Person& p)
	{
		if (this->age == p.age && this->name == p.name)
		{
			return true;
		}
		else {
			return false;
		}
	}
	bool operator!=(Person& p)
	{
		if (this->age == p.age && this->name == p.name)
		{
			return false;
		}
		else {
			return true;
		}
	}
	string name;
	int age;

	
};
void test01()
{
	Person p1("gouride", 19);
	Person p2("gouride", 19);
	if (p1 == p2) {
		cout << "p1和p2相同" << endl;
	}
	else {
		cout << "p1和p2不相同" << endl;
	}
	if (p1 != p2) {
		cout << "p1和p2不相同" << endl;
	}
	else {
		cout << "p1和p2相同" << endl;
	}
}
int main()
{
	test01();
	system("pause");
	return 0;
}

函式呼叫過載

仿函式

#include<iostream>
using namespace std;
#include<string>
class Myprint
{
public:
	void operator()(string name)
	{
		cout << name << endl;
	}
	int operator()(int a,int b)
	{
		return a + b;
	}
};
void test01()
{
	
	Myprint myprint;
	myprint("測試");
	//匿名物件呼叫
	cout << Myprint()(4,6) << endl;
}
int main()
{
	test01();
	system("pause");
	return 0;
}

個人學習記錄,如有錯誤歡迎指正

相關文章