歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> C++的運算符重載詳解

C++的運算符重載詳解

日期:2017/3/1 9:16:27   编辑:Linux編程

什麼是運算符重載?
顧名思義就是將原本的操作符以我們的方式定義出來,方便我們使用。
為什麼要進行運算符重載?
簡單的理由就是將減少程序員的工作量,首先先看一個簡單的例子:
class A{
public:
A(int data):data(data){};
void show(){
cout << "data = " << data << endl;
}
private:
int data;
};
int main(int ac, char *av[])
{
A a1(100), a2(200);

(a1+a2).show(); //請注意這一句我們可以這樣嗎?編譯一下看看
return 0;
}
編譯結果:
[root@anna-laptop day11]# cc.sh overload_operator.cpp
======================== C++_program Compling =====================
overload_operator.cpp: In function ‘int main(int, char**)’:
overload_operator.cpp:17: error: no match for ‘operator+’ in ‘a1 + a2’
ERROR g++ -o overload_operator.cpp -g -lpthread

這樣的結果並不是我們想要的,我們只是想相加一下兩個對象裡面數據並且將結果顯示出來,但是操作符“+”的左右兩邊的變量必須是內置變量類型。所以為了方便,我們可以為我們自定義類型的對象對操作符“+”進行運算符重載,進行如下更改:
class A{
public:
A(int data):data(data){};
void show(){
cout << "data = " << data << endl;
}
// 運算符重載函數
A operator+(const A& a){
return A(data + a.data);
}
private:
int data;
};
int main(int ac, char *av[])
{
A a1(100), a2(200);

(a1+a2).show();
return 0;
}

如我們所願,進行如上更改我們完成了直接讓兩個類對象進行直接相加,大大減少了程序員的工作量。下來我們細細談一下運算符重載函數的具體內容:
運算符重載函數的定義和調用分為兩種:
1、以友元函數定義
定義格式: friend return_val operatorOPT(type& name...);
調用格式:operatorOPT(obj_list);
obj1 OPT obj2;
友元不是成員,不能直接在友元函數中使用對象的成員變量,也不能使用this指針,所以在進行函數調用的時候,需要將對象的成員函數傳進去。

2、以成員函數函數定義
定義格式:return_val operatorOPT(type& name...);
注意:在使用成員函數進行調用的時候,如果使用對象的成員變量,不用將成員變量再傳入函數中,直接在成員函數中使用就可以。
調用格式:obj1.operatorOPT(obj2);
obj1 OPT obj2;
下面舉例來進行說明:
class F{
public:
F(int n = 0, int d = 1):n(n), d(d){}
// 以成員函數對操作符進行重載
F operator*(const F& o)const{
return F(o.n * n, o.d * d);
}
// 以友元函數對操作符進行重載,友元函數的聲明
friend F operator/(const F& obj1, const F& obj2);
private:
int n;
int d;
};

// 友元函數的定義
F operator/(const F& obj1, const F& obj2)
{
return(obj1.n * obj2.d, obj1.d * obj2.n);
}
int main(int ac, char *av[])
{
F f1(1,2);
F f2(3,4);
F f3;

f3 = f1.operator*(f2);
f3.show();
(f1*f2).show();
f3 = operator/(f1, f2);
f3.show();
(f1/f2).show();

return 0;
}

在進行運算符重載的時候我們需要注意兩個問題:
1、在運算符的操作數,必須至少含有一個自定義類型的變量。
2、盡量使用成員函數對運算符進行重載,但是有的運算符只能使用友元函數進行重載。
比如對於"<<" 和“>>”的重載,如下:

class F{
public:
F(int n = 0, int d = 1):n(n), d(d){}
friend ostream& operator<<(ostream& os, const F& f){
os << f.n << '/' << f.d;
return os;
}
friend istream& operator>>(istream& is, F& f){
char ch;
is >> f.n >> ch >> f.d;
return is;
}
F operator*(const F& o)const{
return F(o.n * n, o.d * d);
}
friend F operator/(const F& obj1, const F& obj2);
private:
int n;
int d;
};

F operator/(const F& f1, const F& f2)
{
return(f1.n * f2.d, f1.d * f2.n);
}
int main(int ac, char *av[])
{
F f1(1,2);
F f2(3,4);

cout << f1 << "*" << f2 << "=" << f1*f2 << endl;
cout << f1 << "/" << f2 << "=" << f1/f2 << endl;
cout << "enter 2 number : \n";
cin >> f1 >> f2;
cout << "f1 = " << f1 << endl;
cout << "f2 = " << f2 << endl;

return 0;
}
運行結果:
[root@anna-laptop overload_operator]# ./muldiv
[1/2]*[3/4]=[3/8]
[1/2]/[3/4]=[4/6]
enter 2 number :
123 / 456
234/ 7645
f1 = [123/456]
f2 = [234/7645]

以上的操作符全是對於雙目運算符的重載,下面簡單介紹幾個單目運算符的例子,如"++"和"--"
因為++有前置++和後置++兩種,--也是如此,對於前置++我們直接將++後的結果直接返回即可,對於後置++,為了方便區別於前置++,通常認為++後面仍然含有一個int類型的數組。進行如下操作:
class A{
public:
A(int data = 0):data(data){}
friend ostream& operator<<(ostream& os, const A& a){
os << a.data;
return os;
}
friend istream& operator>>(istream& is, A& a){
is >> a.data;
return is;
}
// 前置 ++;
friend A& operator++(A& a){
a.data += 10;
return a;
}
// 前置 --
A& operator--(){
data -= 10;
return *this;
}
// 後置 ++;
friend A operator++(A& a, int){
A old(a);
a.data += 5;
return old;
}
// 後置 --
A operator--(int){
A old(*this);
data -= 5;
return old;
}
private:
int data;
};
int main(int ac, char *av[])
{
A a1(100);
A a2(100);

cout << "a1 = " << a1 << endl;
cout << "a2 = " << a2 << endl;
++a1;
--a2;
cout << "++a1 = " << a1 << endl;
cout << "--a2 = " << a2 << endl;
cout << "a1++ = " << a1++ << endl;
cout << "a2-- = " << a2-- << endl;
cout << "a1 = " << a1 << endl;
cout << "a2 = " << a2 << endl;
return 0;
}

當然還有以下操作符不能進行重載:
1、三目運算符不能進行重載;
2、"."成員運算符不能重載;
3、成員指針運算符不能進行重載;

4、"::"這是對於類的運算符,不能進行重載。

Copyright © Linux教程網 All Rights Reserved