操作符的重载
#include "stdafx.h"
#include <iostream>
using namespace std;
//可以直接用结构体的名来定义 也可以用结构体类型名
//(struct Complex)来定义
//相当于用typedef过后的类型名
struct Complex
{
float real;
float image;
};
//operator 和 运算符 构成运算符重载
//+号的全局重载函数
Complex operator+(Complex x, Complex y)
{
Complex z;
z.real = x.real + y.real;
z.image = x.image + y.image;
return z;
}
//因为重载的为+运算符 得到的是一个新对象z并返回它
//不能认为是y追加到x上返回x 或者返回y
int _tmain(int argc, _TCHAR* argv[])
{
Complex x = { 1, 2 }, y = { 3, 4 };
Complex z = x + y; //x+y相当于operator+(x,y)
cout << z.real <<","<<z.image << endl;
return 0;
}