1 加号運算符重載
(1)自定義加法資料類型,需要重載+運算符
(2)在成員函數或者全局函數中重寫一個+運算符的函數
(3)函數名 operator+() {}
#include<iostream>
using namespace std;
class Person
{
public:
Person() {}
Person(int a,int b):m_A(a),m_B(b)
{}
/*
// 在類中成員函數重載"+"
Person operator+(Person &p)
{
Person tmp;
tmp.m_A = this->m_A + p.m_A;
tmp.m_B = this->m_B + p.m_B;
return tmp;
}
*/
int m_A;
int m_B;
};
// 全局函數中,重載"+"運算符
Person operator+(Person &p1,Person &p2)
{
Person tmp;
tmp.m_A = p1.m_A + p2.m_A;
tmp.m_B = p1.m_B + p2.m_B;
return tmp;
}
void test()
{
Person p1(1,1),p2(2,2);
Person p3;
p3 = p1 + p2;
cout << p3.m_A << " " << p3.m_B << endl;
}
int main()
{
test();
return 0;
}
輸出:
3 3
2 左移運算符重載
(1)不要随意亂用符号重載
(2)内置資料類型 的運算符不可以重載
(3)cout << 直接對Person自定義資料類型 進行輸出
(4)寫到全局函數中ostream& operator << (ostream &cout,Person & p1) {}
(5)如果重載時候想通路p1的私有成員,那麼全局函數要做Person的友元函數
#include<iostream>
using namespace std;
class Person
{
public:
Person() {}
Person(int a, int b)
{
this->m_A = a;
this->m_B = b;
}
friend ostream &operator<<(ostream&cout, Person &p1);
// 重載左移運算符不可以寫到成員函數中
private:
int m_A;
int m_B;
};
ostream &operator<<(ostream&cout, Person &p1)
{
cout << "p1.m_A=" << p1.m_A << " " << "p1.m_B=" << p1.m_B << endl;
return cout;
}
void test()
{
Person p1(10,10);
cout << p1 << endl;
}
int main()
{
test();
return 0;
}
輸出:
p1.m_A=10 p1.m_B=10
3 前置 後置 ++ 運算符重載
(1)myInt++後置 ++ myInt前置
(2)重載++運算符 operator++()前置 operator++(int) 後置
(3)前置理念 先++ 後傳回自身 後置理念 先儲存住原有值 内部++ 換回臨時資料
#include<iostream>
using namespace std;
class MyInteger
{
public:
MyInteger()
{
m_Num = 0;
}
// 前置++重載
MyInteger& operator++()
{
this->m_Num++;
return *this;
}
// 後置++重載
MyInteger operator++(int)
{
MyInteger tmp = *this;
this->m_Num++;
return tmp;
}
friend ostream& operator<< (ostream& out, MyInteger& myInt);
int m_Num;
~MyInteger()
{
cout << "析構函數" << endl;
}
};
ostream& operator<< (ostream& out, MyInteger& myInt)
{
cout << myInt.m_Num;
return out;
}
void test()
{
MyInteger myInt;
cout << ++myInt << endl;
MyInteger p1 = myInt++;
cout << p1 << endl;
// 如果在vs2017開發環境下,下面會報錯,但是如果在vs2012開發環境下,則不會。
//cout << myInt++ << endl;
cout << myInt << endl;
}
int main()
{
test();
return 0;
}
輸出:
1
析構函數
1
2
析構函數
析構函數
4 智能指針實作
(1)Person類有showAge成員函數
(2)如果new出來的Person對象,需要手動去釋放 delete
(3)有了智能指針,讓智能指針托管這個Person對象,這樣就不用操心了,讓智能指針管理
(4)為了讓智能指針向普通的Person指針一樣使用 就要重載->和
#include <iostream>
using namespace std;
class Person
{
public:
Person(int age)
{
this->m_Age = age;
}
void showAge()
{
cout << "年齡為:" << this->m_Age << endl;
}
~Person()
{
cout << "析構" << endl;
}
int m_Age;
};
// 智能指針
// 用來托管自定義類型的對象,讓對象進行自動的釋放
class smartPointer
{
public:
smartPointer(Person *person)
{
this->person = person;
}
// 重載->讓智能指針對象 向Person *p一樣去使用
Person* operator->()
{
return this->person;
}
Person operator*()
{
return *this->person;
}
~smartPointer()
{
cout << "智能指針析構" << endl;
if (this->person != NULL)
{
delete this->person;
this->person = NULL;
}
}
private:
Person *person;
};
void test()
{
// Person p1(10); //開辟在棧上,自動釋放
// Person *p1 = new Person(10);// 開辟在堆上,手動釋放
// delete p1;
smartPointer sp(new Person(10)); // 開辟到棧上,自動釋放
sp->showAge(); // sp->傳回的是個指針,應該寫成sp->->showAge();單可以這樣寫,因為編譯器給優化了
(*sp).showAge();
}
int main()
{
test();
return EXIT_SUCCESS;
}
輸出:
年齡為:10
年齡為:10
析構
智能指針析構
析構
5 指派運算符重載
(1)系統預設給類提供 指派運算符寫法 是簡單值拷貝
(2)導緻如果類中有指向堆區的指針,就可能出現深淺拷貝的問題
(3)是以要重載 = 運算符
(4)如果想鍊式程式設計 return *this
#include <iostream>
using namespace std;
// 一個類預設建立 預設構造 析構 拷貝構造 opeartor=指派運算符進行簡單的值傳遞
class Person
{
public:
Person(int a)
{
this->m_A = a;
}
Person(const Person &p)
{
cout << "拷貝" << endl;
this->m_A = p.m_A;
}
int m_A;
};
class Person2
{
public:
Person2(const char *name)
{
this->pName = new char[strlen(name)+1];
strcpy(this->pName, name);
}
Person2& operator=(const Person2 &p)
{
// 判斷如果原來已經堆區有内容,先釋放
if (this->pName != NULL)
{
delete[] this->pName;
this->pName = NULL;
}
this->pName = new char[strlen(p.pName) + 1];
strcpy(this->pName, p.pName);
return *this;
}
~Person2()
{
if (this->pName != NULL)
{
delete[] this->pName;
this->pName = NULL;
}
}
char *pName;
};
void test()
{
Person p1(10);
Person p2(0);
p2 = p1;
cout << "p2的m_A" << p2.m_A << endl;
}
void test02()
{
Person2 p1("趙信");
Person2 p2("無極");
Person2 p3(" ");
p3 = p2 = p1;
cout << p2.pName << endl;
cout << p3.pName << endl;
}
int main()
{
//test();
test02();
return EXIT_SUCCESS;
}
輸出:
趙信
趙信
6 關系運算符重載
自定義資料類型 不會内部做比較 == !=,是以要重載
#include<iostream>
#include<string>
using namespace std;
class Person
{
public:
Person(string name, int age)
{
this->m_Name = name;
this->m_Age = age;
}
bool operator==(Person &p)
{
if (this->m_Name == p.m_Name && this->m_Age == p.m_Age)
{
return true;
}
return false;
}
bool operator!=(Person &p)
{
if (this->m_Name != p.m_Name && this->m_Age != p.m_Age)
return true;
return false;
}
string m_Name;
int m_Age;
};
void test()
{
Person p1("小明",10);
Person p2("小強", 9);
Person p3("小強", 9);
if (p1 == p2)
{
cout << "p1和p2相等" << endl;
}
else
{
cout << "p1、p2不相等" << endl;
}
if (p3 == p2)
{
cout << "p2和p3相等" << endl;
}
else
{
cout << "p2、p3不相等" << endl;
}
if (p1 != p2)
{
cout << "p1、p2不相等" << endl;
}
else
{
cout << "p1和p2相等" << endl;
}
}
int main()
{
test();
return 0;
}
輸出:
p1、p2不相等
p2和p3相等
p1、p2不相等
7 函數調用運算符()重載
- ()仿函數 對象() 看似像函數調用-
- MyAdd() 匿名對象
#include<iostream>
#include<string>
using namespace std;
class MyPrint
{
public:
void operator()(string text)
{
cout << text << endl;
}
};
class MyAdd
{
public:
int operator()(int v1, int v2)
{
return v1 + v2;
}
};
void test01()
{
MyPrint myPrint;
myPrint("hello world!"); // 仿函數
}
void test02()
{
MyAdd myAdd;
cout << myAdd(1, 1) << endl;
}
int main()
{
test01();
test02();
return EXIT_SUCCESS;
}
輸出:
hello world!
2
8 符号重載總結
- =、[]、()、->操作符隻能通過成員函數進行重載
- <<和>>隻能通過全局函數和友元函數進行重載
- 不要重載&&和||操作符,因為無法實作短路規則