(1)一般运算符重载
在进行对象之间的运算时候,程序会调用与运算符相对应的函数进行处理,所以运算符重载的方式有两种:成员函数和友元函数。
成员函数的形式比较简单,在类里面定义一个与操作符相关的函数。友元函数因为没有this指针,所以形参会多一个。
class A
{
public:
A(int d):data(d){}
A operator+(A&);//成员函数
A operator-(A&);
A operator*(A&);
A operator/(A&);
A operator%(A&);
friend A operator+(A&,A&);//友元函数
friend A operator-(A&,A&);
friend A operator*(A&,A&);
friend A operator/(A&,A&);
friend A operator%(A&,A&);
private:
int data;
};
{
public:
A(int d):data(d){}
A operator+(A&);//成员函数
A operator-(A&);
A operator*(A&);
A operator/(A&);
A operator%(A&);
friend A operator+(A&,A&);//友元函数
friend A operator-(A&,A&);
friend A operator*(A&,A&);
friend A operator/(A&,A&);
friend A operator%(A&,A&);
private:
int data;
};
(2)关系运算符
关系运算有 ==,!=,< ,<=,>>=等。这里只给出成员函数声明
bool operator ==( const A & );
bool operator !=( const A & );
bool operator <= ( const A & );
bool operator <( const A & );
bool operator >( const A & );
bool operator >=( const A & );
bool operator !=( const A & );
bool operator <= ( const A & );
bool operator <( const A & );
bool operator >( const A & );
bool operator >=( const A & );
(3)逻辑运算符
bool operator || ( const A&);
bool operator && ( const A&);
bool operator !( );
bool operator && ( const A&);
bool operator !( );
(4)单目运算符
这里的 + 、-是正负的意思,放在对象的前面。
A& operator + ();
A& operator -();
A* operator &();
A& operator *();
A& operator -();
A* operator &();
A& operator *();
(5)自增减运算符
++和--根据位置的不同有四种情况,都可以重载
A& operator ++(); //前置++
A operator ++(int ); //后置++
A& operator --(); //前置--
A operator --( int ); //后置--
A operator ++(int ); //后置++
A& operator --(); //前置--
A operator --( int ); //后置--
(6)位运算符重载
按位操作
A operator | ( const A & );
A operator &( const A & );
A operator <<( int i );
A operator >>( int i );
A operator~();
A operator &( const A & );
A operator <<( int i );
A operator >>( int i );
A operator~();
(7) 赋值运算符重载
A& operator +=( const A& );
A& operator -=( const A& );
A& operator *=( const A& );
A& operator /=( const A& );
A& operator %=( const A& );
A& operator &=( const A& );
A& operator |=( const A& );
A& operator ^=( const A& );
A& operator <<=( const A& );
A& operator >>=( const A& );
A& operator -=( const A& );
A& operator *=( const A& );
A& operator /=( const A& );
A& operator %=( const A& );
A& operator &=( const A& );
A& operator |=( const A& );
A& operator ^=( const A& );
A& operator <<=( const A& );
A& operator >>=( const A& );
(8)内存运算符重载
void *operator new(size_t size);
void *operator new(size_t size, int i);
void *operator new[](size_t size);
void operator delete(void*p);
void operator delete(void*p, int i, int j);
void operator delete [](void* p);
void *operator new(size_t size, int i);
void *operator new[](size_t size);
void operator delete(void*p);
void operator delete(void*p, int i, int j);
void operator delete [](void* p);
(9)只能以友元函数的形式重载
friend inline ostream &operator << (ostream&, A&);//输出流
friend inline istream &operator >> (istream&, A&);//输入流
friend inline istream &operator >> (istream&, A&);//输入流
(10)只能是成员函数
A& operator = (const A& );
char operator [] (int i);
const char* operator () ();
T operator -> ();
char operator [] (int i);
const char* operator () ();
T operator -> ();
(11)类型转换符
operator char* () const;
operator int ();
operator const char () const;
operator short int () const;
operator long long () const;
//还有很多就不写了
operator int ();
operator const char () const;
operator short int () const;
operator long long () const;
//还有很多就不写了
来源:https://www.cnblogs.com/gt123/p/3448139.html