📜  C++中可以重载和不能重载的运算符有哪些?

📅  最后修改于: 2022-05-13 01:55:38.054000             🧑  作者: Mango

C++中可以重载和不能重载的运算符有哪些?

通过实现以下任何类型的函数,有多种方法可以在 C++ 中重载运算符:

1) 成员函数

2) 非会员函数

3) 好友函数

可以重载的运算符列表有:

+

 *

%

 &

|

 ~

!

=

 <

>

+=

-=

 *=

⁄=

%=

‸=

&=

|=

 <<

 >>

<<=

>>=

==

 !=

<=

>=

&&

||

 ++

 —

,

->*

->

( )

[ ]

new

delete

new[]

delete[]

 

  

示例 1:重载 ++ 运算符

CPP
// CPP program to illustrate
// operators that can be overloaded
#include 
using namespace std;
  
class overload {
private:
    int count;
  
public:
    overload()
        : count(4)
    {
    }
  
    void operator++() { count = count + 1; }
    void Display() { cout << "Count: " << count; }
};
  
int main()
{
    overload i;
    // this calls "function void operator ++()" function
    ++i;
    i.Display();
    return 0;
}


CPP
// CPP program to demonstrate the
// Difference between pre increment 
// and post increment overload operator
#include 
using namespace std;
  
class overload {
private:
    int count;
  
public:
    overload(int i)
        : count(i)
    {
    }
  
    overload operator++(int) { return (count++); }
    overload operator++()
    {
        count = count + 1;
        return count;
    }
    void Display() { cout << "Count: " << count << endl; }
};
// Driver code
int main()
{
    overload i(5);
    overload post(5);
    overload pre(5);
  
    // this calls "function overload operator ++()" function
    pre = ++i;
    cout << "results of I   =   ";
    i.Display();
    cout << "results of preincrement   =  ";
    pre.Display();
    // this call "function overload operator ++()"function
    i++; // just to show diff
    i++; // just to show diff
    post = i++;
    cout << "Results of post increment   =   ";
    post.Display();
    cout << "And results of i , here we see difference   : "
            "  ";
    i.Display();
    return 0;
}


CPP
// CPP program to illustrate overloading the
// [ ] operator
  
#include 
using namespace std;
class overload {
    int a[3];
  
public:
    overload(int i, int j, int k)
    {
        a[0] = i;
        a[1] = j;
        a[2] = k;
    }
    int operator[](int i) { return a[i]; }
};
int main()
{
    overload ob(1, 2, 3);
    cout << ob[1]; // displays 2
    return (0);
}


CPP
// CPP program to illustrate
// operators that can be overloaded
#include 
using namespace std;
  
class GFG {
public:
    int num;
    GFG(int j) { num = j; }
    GFG* operator->(void) { return this; }
};
  
// Driver code
int main()
{
    GFG T(5);
    GFG* Ptr = &T;
  
    // Accessing num normally
    cout << "T.num = " << T.num << endl;
  
    // Accessing num using normal object pointer
    cout << "Ptr->num = " << Ptr->num << endl;
  
    // Accessing num using -> operator
    cout << "T->num = " << T->num << endl;
  
    return 0;
}


CPP
// C++ program to illustrate
// Overloading this .(dot) operator
#include 
using namespace std;
  
class cantover {
public:
    void fun();
};
class X {
    cantover* p;
    cantover& operator.() { return *p; }
    void fun();
};
void g(X& x)
{
    x.fun(); // X::fun or cantover::fun or error?
}


输出
Count: 5

当 ++运算符对重载类的对象(在本例中为对象 i)进行操作时,将调用此函数。在程序中,定义了void 运算符 ++()运算符函数(在重载类内部)。此函数将 i 对象的 count 值增加 1。

示例 2:重载 ++运算符,即前后自增运算符

CPP

// CPP program to demonstrate the
// Difference between pre increment 
// and post increment overload operator
#include 
using namespace std;
  
class overload {
private:
    int count;
  
public:
    overload(int i)
        : count(i)
    {
    }
  
    overload operator++(int) { return (count++); }
    overload operator++()
    {
        count = count + 1;
        return count;
    }
    void Display() { cout << "Count: " << count << endl; }
};
// Driver code
int main()
{
    overload i(5);
    overload post(5);
    overload pre(5);
  
    // this calls "function overload operator ++()" function
    pre = ++i;
    cout << "results of I   =   ";
    i.Display();
    cout << "results of preincrement   =  ";
    pre.Display();
    // this call "function overload operator ++()"function
    i++; // just to show diff
    i++; // just to show diff
    post = i++;
    cout << "Results of post increment   =   ";
    post.Display();
    cout << "And results of i , here we see difference   : "
            "  ";
    i.Display();
    return 0;
}
输出
results of I   =   Count: 6
results of preincrement   =  Count: 6
Results of post increment   =   Count: 8
And results of i , here we see difference   :   Count: 9

示例 3:重载 [ ]运算符

CPP

// CPP program to illustrate overloading the
// [ ] operator
  
#include 
using namespace std;
class overload {
    int a[3];
  
public:
    overload(int i, int j, int k)
    {
        a[0] = i;
        a[1] = j;
        a[2] = k;
    }
    int operator[](int i) { return a[i]; }
};
int main()
{
    overload ob(1, 2, 3);
    cout << ob[1]; // displays 2
    return (0);
}
输出
2

示例 4:重载 ->运算符

CPP

// CPP program to illustrate
// operators that can be overloaded
#include 
using namespace std;
  
class GFG {
public:
    int num;
    GFG(int j) { num = j; }
    GFG* operator->(void) { return this; }
};
  
// Driver code
int main()
{
    GFG T(5);
    GFG* Ptr = &T;
  
    // Accessing num normally
    cout << "T.num = " << T.num << endl;
  
    // Accessing num using normal object pointer
    cout << "Ptr->num = " << Ptr->num << endl;
  
    // Accessing num using -> operator
    cout << "T->num = " << T->num << endl;
  
    return 0;
}
输出
T.num = 5
Ptr->num = 5
T->num = 5

不能重载的运算符列表

1) 范围解析运算符 (::)

2)三元或条件运算符 (?:)

3)会员访问或点运算符(.)

4)指向成员运算符 (.*)

5)对象大小运算符(sizeof)

6)对象类型运算符(typeid)

示例 5:重载此 .(dot)运算符

点(.)运算符不能重载,所以会产生错误。

CPP

// C++ program to illustrate
// Overloading this .(dot) operator
#include 
using namespace std;
  
class cantover {
public:
    void fun();
};
class X {
    cantover* p;
    cantover& operator.() { return *p; }
    void fun();
};
void g(X& x)
{
    x.fun(); // X::fun or cantover::fun or error?
}

输出:错误

prog.cpp:12:23: error: expected type-specifier before ‘.’ token
    cantover& operator.() { return *p; }

该程序将产生错误。同样,上述运算符如果重载也会产生错误。