运算符重载

用了好久的运算符重载,但记忆里面一直不是很清晰~ @_@

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 using namespace std;
 5 
 6 class ca
 7 {
 8 public:    
 9     int value;
10     //重载为成员函数格式
11     int operator+(const ca &v){
12         return this->value + v.value; // 等同于return value+v.value;
13     }
14 };
15 
16 //重载为非成员函数格式
17 int operator+(const ca &v1, const ca &v2)
18 {
19     return v1.value + v2.value;
20 }
21 
22 int main()
23 {
24     ca a, b;
25 
26     a.value = 10;
27     b.value = 20;
28 
29     printf("a+b:%d
", a + b);    // 优先用成员函数
30     
31     return 0;
32 }
原文地址:https://www.cnblogs.com/doggod/p/9298708.html