四种不能重载的运算符:
1> :: 域运算符
2> . 成员访问运算符
3> * 成员 指针访问运算符((*this).member)
4>:? 三目运算符
只能用友元重载的运算符:
1> << 输出运算符重载
2> >> 输入运算符重载
即能声明友元又能声明为类的成员函数的运算符重载:
1> 四则运算符
2> ++ --运算符
3> != == += -= *= /= 运算符
只能声明为类的成员函数的运算符
1>[] 下标运算符
2>()
3> ->
4>= 赋值运算符
一般不用重载的运算符
1>=
2>&
3>*
另外重载只能是现有的运算符,且重载后不能改变运算符的优先级。
下面是一个例子:
// // Complex.h // train5 // // Created by student on 15/8/11. // Copyright (c) 2015年 personals. All rights reserved. // #ifndef __train5__Complex__ #define __train5__Complex__ #include <iostream> using namespace std; class Complex{ public: Complex(int r1=0,int i1=0); ~Complex(){} const Complex& operator--(); //前置减减(实部减一,虚部不变) const Complex operator--(int); //后置减减 friend const Complex operator+(const Complex& p1,const Complex& p2); friend const Complex operator-(const Complex& p1,const Complex& p2); friend const Complex operator*(const Complex& p1,const Complex& p2); friend const Complex operator/(const Complex& p1,const Complex& p2); friend Complex operator+=(Complex& c1,const Complex& c2); friend Complex operator-=(Complex& c1,const Complex& c2); friend Complex operator*=(Complex& c1,const Complex& c2); friend Complex operator/=(Complex& c1,const Complex& c2); friend ostream& operator<<(ostream& out,const Complex& c1); friend bool operator==(const Complex& c1,const Complex& c2); friend bool operator!=(const Complex& c1,const Complex& c2); private: int r,i; }; #endif /* defined(__train5__Complex__) */
// // Complex.cpp // train5 // // Created by student on 15/8/11. // Copyright (c) 2015年 personals. All rights reserved. // #include "Complex.h" Complex::Complex(int r1,int i1){ r=r1; i=i1; } const Complex operator+(const Complex& p1,const Complex& p2){ return Complex(p1.r+p2.r,p1.i+p2.i); } const Complex operator-(const Complex& p1,const Complex& p2){ return Complex(p1.r-p2.r,p1.i-p2.i); } const Complex operator*(const Complex& p1,const Complex& p2){ return Complex(p1.r*p2.r-p1.i*p2.i,p1.r*p2.i+p1.i*p2.r); } const Complex operator/(const Complex& p1,const Complex& p2){ float den=p2.r*p2.r+p2.i*p2.i; return Complex((p1.r*p2.r+p1.i*p2.i)/den,(-p1.r*p2.i+p1.i*p2.r)/den); } Complex operator+=(Complex& c1,const Complex& c2){ c1=c1+c2; return c1; } Complex operator-=(Complex& c1,const Complex& c2){ c1=c1-c2; return c1; } Complex operator*=(Complex& c1,const Complex& c2){ c1=c1*c2; return c1; } Complex operator/=(Complex& c1,const Complex& c2){ c1=c1/c2; return c1; } /*const Complex& Complex::operator--(){ this->r-=1; return *this; }*/ /*const Complex Complex::operator--(int){ Complex old = (*this); --(*this); return old; }*/ const Complex& Complex::operator--(){ Complex c(1,0); (*this)=(*this)-c; return *this; } const Complex Complex::operator--(int){ Complex old = (*this); Complex c(1,0); (*this)=(*this)-c; return old; } ostream& operator<<(ostream& out,const Complex& c1){ out<<"r="<<c1.r<<",i="<<c1.i<<endl; return out; } bool operator==(const Complex& c1,const Complex& c2){ if ((c1.r==c2.r) && (c1.i==c2.i)) { return true; } else{ return false; } } bool operator!=(const Complex& c1,const Complex& c2){ if ((c1.r==c2.r) && (c1.i==c2.i)) { return false; } else{ return true; } }
// // main.cpp // train5 // // Created by student on 15/8/11. // Copyright (c) 2015年 personals. All rights reserved. // #include <iostream> #include "Complex.h" int main(int argc, const char * argv[]) { // insert code here... Complex c1(1,3),c2(4,5); Complex c; c1+=c2; cout<<c1; c1-=c2; cout<<c1; c1*=c2; cout<<c1; c1/=c2; cout<<c1; c1--; --c1; cout<<c1; if (c1==c2) { cout<<"c1==c2\n"; } else if (c1!=c2){ cout<<"c1!=c2\n"; } //std::cout << "Hello, World!\n"; return 0; }
下面是对运算符()和->的重载,它们的作用分别是,赋值操作和普通对象也能调用 ->
// // Complex.h // 重载运算符 // // Created by student on 15/8/16. // Copyright (c) 2015年 personals. All rights reserved. // #ifndef _________Complex__ #define _________Complex__ #include <iostream> using namespace std; class Complex{ public: Complex(float x=0,float y=0){ r=x;i=y; } ~Complex(){} const Complex& operator()(const Complex& c); const Complex* operator->(); friend ostream& operator<<(ostream& os,const Complex& c); void print() const{ cout<<"r="<<r<<",i="<<i<<endl; } private: float r,i; }; #endif /* defined(_________Complex__) */
// // Complex.cpp // 重载运算符 // // Created by student on 15/8/16. // Copyright (c) 2015年 personals. All rights reserved. // #include "Complex.h" const Complex& Complex::operator()(const Complex &c){ i = c.i; r = c.r; cout<<"Operator () is called!\n"; return *this; } const Complex* Complex::operator->(){ cout<<"Opeartor -> is called!\n"; return this; } ostream& operator<<(ostream& os,const Complex& c){ os<<"r="<<c.r<<",i="<<c.i<<endl; return os; }
// // main.cpp // 重载运算符 // // Created by student on 15/8/16. // Copyright (c) 2015年 personals. All rights reserved. // #include <iostream> #include "Complex.h" int main(int argc, const char * argv[]) { // insert code here... Complex c1,c2(2,3); c1(c2); c1->print(); cout<<c1<<c2; //std::cout << "Hello, World!\n"; return 0; }