原文地址:http://www.cnblogs.com/wzh206/archive/2010/03/25/1696162.html
定義格式
返回類型 operator運算符(形式參數表) { 函數體 }
參數個數的限定
非成員函數:
單目運算符:參數表中只有一個參數;
雙目運算符:參數表中只有兩個參數
成員函數:
單目運算符:參數表中沒有參數;
雙目運算符:參數表中只有一個參數
不能重載的運算符
1、不能重載的運算符有: ::, ., .*, ?:
2、必須重載爲成員函數的運算符: [], (), –>, =
3、在類成員函數中重載運算符是不允許返回引用的,會出現“返回局部變量的地址”警告
4、cout << f1 << f2;
//用重載運算符表示,只能通過友員來實現
//如果要用成員函數,則會有cout.operator<<(const F& f),所以這是不
// 可能的.因此只能用友員來實現,operator<<(cout,f)
// 而cout是ostream型的,因此有以下標準格式.注意不能加const,因爲
//cout是要改變的,會改變裏的緩衝成員.
ostream& operator<<( /* 不能加const */ ostream& cout, constF&) //輸出運算符的標準重載格式.
friend istream& operator>>(istream& is, F& f){ }//輸入運算符重載標準格式
//重載運算符完整例子
//雙目運算符重載
#include <iostream> using namespace std; class F{ int n; int d; 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; } F operator*(const F& o) { return F(n*o.n,d*o.d); } friend F operator/(const F& f1,const F& f2){ return F(f1.n*f2.d,f1.d*f2.n); } friend istream& operator>>(istream& is, F& f){ char ch; is >> f.n >> ch >> f.d; return is; } }; int main() { F f1(3,5),f2(11,7),f; cout << f1 << '*' << f2 << '=' << f1*f2 << endl; cout << f1 << '/' << f2 << '=' << f1/f2 << endl; cout << "Input 2 fractions :"; cin >> f1 >> f2; cout <<"f1=" << f1 << endl; cout << "f2=" << f2 << endl; }
* 單目運算符重載
-友元函數形式,返回類型 operatorX(形參)
使用:X obj ---> operatorX(obj);
-成員函數形式 儘量用成員 返回類型 operatorX(/*無形參*/)
使用: X obj ---> obj.operator();
//單目運算符重載
//把後++,後--當作雙目運算符,第二個操作數是整形.
#include <iostream> using namespace std; class A{ int data; public : A(int d=0):data(d){} 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 += 1; return old; } A /* 不能用引用 */ operator--(int){ A old(*this); data -= 1; return old; } }; int main() { A a1(50),a2(100); cout << "a1=" <<a1 << endl; cout << "a2=" <<a2 << endl; cout << "++a1=" << ++a1 << endl; cout << "--a1=" << --a1 << endl; cout << "a1++=" << a1++ << endl; cout << "a1=" <<a1 << endl; cout << "a2--=" << a2-- << endl; cout << "a2=" <<a2 << endl; }
運算符重載提供了一個自己規定運算符式作方式的方法,至少有一個操
作數是自定義類型的.基本類型我們是規定不了的.
強制類型轉換:類型(數據) --> (不必寫返回類型,因爲始終與後面的類
型是相同的) operator類型(無形參) 只能寫成成員函數,不能是友員.
#include<iostream>
using namespacestd;
classA{
intdata;
public:
A(intd=0):data(d){}
operator int(){
returndata;
}
operator bool(){
returndata!=0;
}
operator char(){
return(char)data;
}
};
intmain()
{
A a1(65),a2(200);
cout << "a1="<< (char)a1 << endl;
intd=a2;
if(a2)
cout << "good"<< endl;
}
對自定義類型的對象,使用運算符時,總是調用相應的運算符函數
三目運算符不能重載.
等號是雙目運算符也可重載,它也只能是成員.
還有點號('.')不能重載.
雙冒號(::)不能重載.
sizeof(類型)沒法重載.
#號不是運算符,無所謂重載.
'= () [] -> 類型轉換'只能是成員函數來重載,其它的隨便,我們建議儘量
使用成員函數來寫,有點只能用友元,比如輸入輸出.
==================================================
+ 雙目運算符重載
- 友元形式:返 operator符號(形1,形2)
- 成員形式:返 operator符號(形)
+ 單目運算符重載
- 友元: 返 operator符號(形參)
- 成員: 返 operator符號()
+ 特例
- 加加
- 先加加
- 後加加 返回 operator符號(形,int)
- 減減
- 先減減
- 後減減