📜  在C++中可以重载的运算符是什么?

📅  最后修改于: 2021-05-30 03:58:08             🧑  作者: Mango

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

+    -    *    /      %        ^
&    |    ~    !,        =
    =      ++        --
    ==    !=      &&        ||
+=    -=    /=    %=      ^=        &=
|=    *=    =      []        ()
->    ->*    new    new []      delete    delete []

示例1:重载++运算符

// 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;
}

输出:

Count: 5

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

示例2:重载++运算符和重载postincrement运算符

// this program will demonstrate 
// Difference between pre increment and post 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<
output:
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:重载此[]运算符

#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 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;
}
// Thank you Rohan Agarwal for suggesting this example.

输出 :

T.num = 5
Ptr->num = 5
T->num = 5

不能重载的运算符列表

1> Scope Resolution Operator  (::)    
2> Pointer-to-member Operator (.*)    
3> Member Access or Dot operator  (.)    
4> Ternary or Conditional Operator (?:) 
5> Object size Operator   (sizeof) 
6> Object type Operator   (typeid) 

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

点(。)运算符不能重载,因此会导致错误。

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

输出

prog.cpp:8:27: error: expected type-specifier before '.' token
         cantover& operator.() 
                           ^
prog.cpp:8:19: error: expected ';' at end of member declaration
         cantover& operator.() 
                   ^
prog.cpp:8:27: error: expected unqualified-id before '.' token
         cantover& operator.() 
                           ^
prog.cpp: In function 'void g(X&)':
prog.cpp:13:14: error: 'void X::fun()' is private
         void fun();
              ^
prog.cpp:18:15: error: within this context
         x.fun();              // X::fun or cantover::fun or error?
               ^

这个问题可以通过几种方式解决。在标准化时,尚不清楚哪种方法最好。

要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”