通过实现以下任何类型的函数,有多种方法在C++中加载运算符:
1) 成员函数
2) 非成员函数
3) 友元函数
可以重载的运算符列表包括:
+ |
– |
* |
⁄ |
% |
‸ |
& |
| |
~ |
! |
= |
< |
> |
+= |
-= |
*= |
⁄= |
%= |
‸= |
&= |
|= |
<< |
>> |
<<= |
>>= |
== |
!= |
<= |
>= |
&& |
|| |
++ |
— |
, |
->* |
-> |
( ) |
[ ] |
新 |
删去 |
新的[] |
删除[] |
|
示例1:重载++运算符
CPP
// CPP program to illustrate // operators that can be overloaded #include <iostream> 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:重载++运算符,即前置和后置增量运算符
CPP
// CPP program to demonstrate the // Difference between pre increment // and post increment overload operator #include <iostream> 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 <iostream> 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 <bits/stdc++.h> 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:重载这个。(点)运算符
点(.)运算符不能重载,因此它将生成一个错误。
CPP
// C++ program to illustrate // Overloading this .(dot) operator #include <iostream> 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; }
此程序将生成一个错误。同样,如果重载,上述运算符也会生成错误。
本文由 希瓦尼·古泰尔 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。