C++運算子過載詳細解說及程式碼編寫
阿新 • • 發佈:2018-11-01
一、不能過載的運算子:
(1) "."(類成員訪問運算子)
(2)" .*"(類成員指標訪問運算子)
(3) "::"(域運算子)
(4)"sizeof"(長度運算子)
(5) " ?:"(條件運算子) 二、運算子 1.算術運算子 + - * / %
2.關係運算符 > < >= <=
3.邏輯運算子 && || !
4.自增 自減 (前++ 後++) (前-- 後--)
5.位運算子 & |
6.賦值運算子 += -= ==
7.輸入輸出運算子 >> <<
7.其他運算子 () [] * & -> 取負-。。。 三、運算子過載:
1.不能定義新的運算子,只能過載已有的運算子
2.過載之後的運算子的優先順序和結合性都不改變
3.不能改變原運算子所需操作符的個數,同時至少要有一個運算元是自定義型別的運算元
4.運算子過載後原語義沒消失,只相當於針對特定的類定義了一個新的運算子 四、友元運算子過載和成員運算子過載的主要區別:
1.引數個數不同
2.友元函式沒有this指標
3.當運算子的左運算元是一個常數時,就不能利用this指標,應當用友元函式過載,例子見過載減運算子“+” 若運算子是一元的,則引數表為空,此時當前物件作為此運算子的單運算元;
若運算子是二元的,則引數表中有一個運算元,此時當前物件作為此運算子的左運算元,引數表中的運算元作為此運算子的右運算元,以此類推。 五、原始碼示例
(1) "."(類成員訪問運算子)
(2)" .*"(類成員指標訪問運算子)
(3) "::"(域運算子)
(4)"sizeof"(長度運算子)
(5) " ?:"(條件運算子) 二、運算子 1.算術運算子 + - * / %
2.關係運算符 > < >= <=
3.邏輯運算子 && || !
4.自增 自減 (前++ 後++) (前-- 後--)
5.位運算子 & |
6.賦值運算子 += -= ==
7.輸入輸出運算子 >> <<
7.其他運算子 () [] * & -> 取負-。。。 三、運算子過載:
1.不能定義新的運算子,只能過載已有的運算子
2.過載之後的運算子的優先順序和結合性都不改變
3.不能改變原運算子所需操作符的個數,同時至少要有一個運算元是自定義型別的運算元
4.運算子過載後原語義沒消失,只相當於針對特定的類定義了一個新的運算子 四、友元運算子過載和成員運算子過載的主要區別:
1.引數個數不同
2.友元函式沒有this指標
3.當運算子的左運算元是一個常數時,就不能利用this指標,應當用友元函式過載,例子見過載減運算子“+” 若運算子是一元的,則引數表為空,此時當前物件作為此運算子的單運算元;
若運算子是二元的,則引數表中有一個運算元,此時當前物件作為此運算子的左運算元,引數表中的運算元作為此運算子的右運算元,以此類推。 五、原始碼示例
以上便是C++運算子過載的內容,希望對你有所幫助,歡迎在下方評價交流#include<iostream> using namespace std; struct st { int x; }; st s; class A { int x; int arr[10]; public: A() { x = 0; cout << "呼叫無參構造" << endl; } A(int x) :x(x) { cout << "呼叫有參構造" << endl; } A(const A& other) :x(other.x) { cout << "呼叫拷貝構造" << endl; } ~A() { cout << "呼叫解構函式" << endl; } //過載算術運算子 A operator+(const A& other); A operator-(const A& other); A operator*(const A& other); A operator/(const A& other); A operator%(const A& other); //過載關係運算符 friend bool operator>(const A& a, const A& b); friend bool operator<(const A& a, const A& b); friend bool operator>=(const A& a, const A& b); friend bool operator<=(const A& a, const A& b); //自增自減 A& operator++();//前++ A operator++(int);//後++ 引數int不需要傳參 與前++區分開 A& operator--();//前-- A operator--(int);//後-- //賦值運算子 A& operator+=(const A& other); A& operator-=(const A& other); bool operator==(const A& other); //輸入輸出運算子 friend istream& operator >> (istream& is, A& other);//不可以用const A& 否則報錯 因為other應為可修改的變數 const常量會導致other不可修改 friend ostream& operator << (ostream& os, const A& other); //其他運算子() [] * & -> 取負 - void operator()(int x, int y); int& operator[](size_t index); int& operator*(); int* operator&(); st* operator->(); A operator-(); }; //過載算術運算子 //+運算子過載 A A::operator+(const A& other) { return A(this->x+other.x); } //-運算子過載 A A::operator-(const A&other)//類外定義成員函式 函式名前面加上類名:: { return A(this->x - other.x); } //*運算子過載 A A::operator*(const A&other) { return A(this->x*other.x); } // /運算子過載 A A::operator/(const A&other) { return A(this->x / other.x); } //%運算子過載 A A::operator%(const A&other) { return A(this->x%other.x); } //過載關係運算符 //>運算子過載 bool operator>(const A&a, const A&b) { return a.x > b.x; } //<運算子過載 bool operator<(const A&a, const A&b) { return a.x < b.x; } //>=運算子過載 bool operator>=(const A&a, const A&b) { return a.x >= b.x; } //<=運算子過載 bool operator<=(const A&a, const A&b) { return a.x <= b.x; } //自增自減 //前++ A& A::operator++() { ++this->x; return *this;//返回值是引用,即返回的是物件本身,而不是臨時物件 } //返回引用和不返回引用 區別在於是否需要產生臨時物件 ++++++a時,返回不是引用會不能連續前++ 如++++++a結果依舊為 1 //返回引用保證了地址在上一次前++處 也就意味著是在前一次++的基礎上再++ 如++++++a結果為 3 //引數int不需要傳參 int用於區分前後++ //後++ A A::operator++(int) { return A(this->x++);//不能返回物件本身 應返回臨時物件(返回型別不是引用) 呼叫完後被析構先讀取到this->x 表現出延遲性 } //前-- A& A::operator--() { --this->x; return *this; } //後-- A A::operator--(int) { return A(this->x--); } //賦值運算子 //+=運算子 A& A::operator+=(const A& other) { this->x += other.x; return *this; } //-=運算子 A& A::operator-=(const A& other) { this->x -= other.x; return *this; } //==運算子 bool A::operator==(const A& other) { return this->x == other.x; } //過載輸入輸出 //過載>> istream& operator >> (istream& is, A& other) { is >> other.x; return is; } //過載<< ostream& operator << (ostream& os, const A& other) { os << other.x; return os; } //其他運算子 () [] * & -> 取負- //() void A::operator()(int x, int y) { cout << "假裝自己是函式名" << endl; cout << x << '\t' << y << endl; } //[] int& A::operator[](size_t index) { return arr[index]; } //* int& A::operator*() { return arr[0];//*arr 陣列的地址 } //& int* A::operator&()//過載& 取地址 要求返回地址 這個地址是什麼地址都可以 { return arr; } //-> st* A::operator->()//一般用於結構體指標 物件指標 { return &s;//返回結構體指標 } A A::operator-() { A a(-this->x); return a; } int main() { A a, b,c; ++++++a; cout << "++++++a=" << a << endl; b=a++; cout << "b=" << b <<'\t'<< "a=" << a << endl; c = a + b; cout << "c=a+b=" << c << endl; c = a - b; cout << "c=a-b=" << c << endl; c = a*b; cout << "c=a*b=" << c << endl; c = a / b; cout << "c=a/b=" << c << endl; c = a % b; cout << "c=a%b=" << c << endl; a += b; cout << "a+=b:" << a << endl; a -= b; cout << "a-=b:" << a << endl; if (a == b) { cout << "a==b"<<endl; } else { cout << "a!=b" << endl; } a(3,4); a[2] = 2; cout << "a[2]=" << a[2] << endl; a[0] = 1; *a = a[0]; cout << "*a=" << *a << endl; cout << "&a=" << &a << endl; a->x = 8; cout << "a->x=" << a->x << endl; cin.get(); return 0; }