操作符重载的概念
我们先来看下面这个例子是否能编译通过 实现复数的相加 # include <stdio.h> class Complex { public : int a ; int b ; } ; int main ( ) { Complex c1 = { 1 , 2 } ; Complex c2 = { 3 , 4 } ; Complex c3 = c1 + c2 ; return 0 ; } 结果: sice@sice:~$ g++ c.cpp c.cpp: 在函数‘int main ( ) ’中: c.cpp:12:22: 错误: ‘operator+’在‘c1 + c2’中没有匹配 显然C++不支持这样对象直接相加 改进(1): # include <stdio.h> class Complex { private : int a ; int b ; public : Complex ( int a = 0 , int b = 0 ) { this - > a = a ; this - > b = b ; } int getA ( ) { return a ; } int getB ( ) { return b ; } friend Complex Add ( Complex & p1 , Complex & p2 ) ; } ; Complex Add ( Complex & p1 ,