标签:
学习C++ 我们最深刻的体会就是C++不像那个C同学,是那样的单纯,我们表面上一看就透,是啥就是傻。C++中所有的东西都是为面向对象而设计的,所以不免有些黑盒之类到的思想,就是很多表面的东西,在内部是经过复杂的封装的,所以这个C++同学已经别社会污染,变得城府很深了,我们要想了解这位C++同学,就必须从他的外表之下去偷窥他内心那单纯的性格,只有这样才能看到它的内心世界!
今天我想讨论一下运算符重载,其实说白了就是一个函数重载,只是这个函数的名字有点怪而已。看这一点就可以体现出C++ 同学的城府很深吧,在C语言中如果重名了,就不认识了,但是对于C++ 同学他可是可以看透重名的不同哟。
今天只思考一下,既然运算符重载是函数重载,那对于C++来说到底用 成员函数,普通函数,还是友员函数进行重载呢
今天拿复数的四则运算作为一个例子:
1.运算符重载作为普通函数:
/********************************************************************** * * Copyright (c)2015,WK Studios * * Filename: A.cpp * * Compiler: GCC vc 6.0 * * Author:WK * * Time: 2015 4 5 * **********************************************************************/ #include<iostream> using std::cout; using std::cin; class Complex { public: double real; double imag; private: }; //普通函数(没有this指针只能访问类的公有部分) Complex operator+(Complex &a,Complex &b); Complex operator-(Complex &a,Complex &b); Complex operator*(Complex &a,Complex &b); Complex operator/(Complex &a,Complex &b); int main() { Complex com1(1.1,2.3),com2(2.2,4.3); Complex total; total=com1/com2;//隐式调用 cout<<total.real<<" "<<total.imag<<"\n"; total=operator/(com1,com2);//与上面的等价,这是显式调用 cout<<total.real<<" "<<total.imag<<"\n"; return 0; } Complex operator+(Complex &a,Complex &b) { return Complex(a.real+b.real,a.imag+b.imag); } Complex operator-(Complex &a,Complex &b) { return Complex(a.real-b.real,a.imag-b.imag); } Complex operator*(Complex &a,Complex &b) { return Complex(a.real*b.real-a.imag*b.imag,a.real*b.imag+a.imag*b.real); } Complex operator/(Complex &a,Complex &b) { return Complex((a.real*b.real+a.imag*b.imag)/(b.real*b.real+b.imag*b.imag),(b.real*a.imag-a.real*b.imag)/(b.real*b.real+b.imag*b.imag)); }
2.运算符重载作为成员函数:
/********************************************************************** * * Copyright (c)2015,WK Studios * * Filename: A.h * * Compiler: GCC vc 6.0 * * Author:WK * * Time: 2015 4 5 * **********************************************************************/ #include<iostream> using std::cout; using std::cin; class Complex { public: //构造函数 Complex(double r=0.0,double i=0.0):real(r),imag(i) {} //复数加 //成员函数 Complex operator+(Complex &b); //复数减 //成员函数 Complex operator-(Complex &b); //复数乘 //成员函数 Complex operator*(Complex &b); //复数除 //成员函数 Complex operator/(Complex &b); void print() { cout<<real<<" "<<imag<<"\n"; } private: double real; double imag; }; int main() { Complex com1(1.1,2.3),com2(2.2,4.3); Complex total; total=com1+com2;//隐式调用 total.print(); total=com1.operator+(com2);////与上面的等价,这是显式调用 total.print(); return 0; } Complex Complex ::operator+(Complex &b) { return Complex(real+b.real,imag+b.imag); } Complex Complex:: operator-(Complex &b) { return Complex(real-b.real,imag-b.imag); } Complex Complex:: operator*(Complex &b) { return Complex(real*b.real-imag*b.imag,real*b.imag+imag*b.real); } Complex Complex:: operator/(Complex &b) { return Complex((real*b.real+imag*b.imag)/(b.real*b.real+b.imag*b.imag),(b.real*imag-real*b.imag)/(b.real*b.real+b.imag*b.imag)); }</span>
成员函数对于很多运算符都要重载为这种形式,使用较多
3.运算符重载作为友员函数:
友员函数只属于类,不属于任何对象所以他不需要对象的this指针来确定自己的数据,但是没有指针this就不能使用,在这里这个有点像普通函数,只是只是友员函数时类的朋友,可以访问类的私密数据,单是具体什么时候使用友员函数最好,这是因时而宜!
/********************************************************************** * * Copyright (c)2015,WK Studios * * Filename: A.h * * Compiler: GCC vc 6.0 * * Author:WK * * Time: 2015 4 5 * **********************************************************************/ #include<iostream> using std::cout; using std::cin; class Complex { public: //构造函数 Complex(double r=0.0,double i=0.0):real(r),imag(i) {} //友员函数(无this指针) friend Complex operator+(Complex &a,Complex &b); //友员函数(无this指针) friend Complex operator-(Complex &a,Complex &b); //友员函数(无this指针) friend Complex operator/(Complex &a,Complex &b); //友员函数(无this指针) friend Complex operator*(Complex &a,Complex &b); void print() { cout<<real<<" "<<imag<<"\n"; } private: double real; double imag; }; int main() { Complex com1(1.1,2.3),com2(2.2,4.3); Complex total; total=com1+com2;//隐式调用 total.print(); total=operator+(com1,com2);////与上面的等价,这是显式调用 total.print(); return 0; } Complex operator+(Complex &a,Complex &b) { return Complex(a.real+b.real,a.imag+b.imag); } Complex operator-(Complex &a,Complex &b) { return Complex(a.real-b.real,a.imag-b.imag); } Complex operator*(Complex &a,Complex &b) { return Complex(a.real*b.real-a.imag*b.imag,a.real*b.imag+a.imag*b.real); } Complex operator/(Complex &a,Complex &b) { return Complex((a.real*b.real+a.imag*b.imag)/(b.real*b.real+b.imag*b.imag),(b.real*a.imag-a.real*b.imag)/(b.real*b.real+b.imag*b.imag)); }
标签:
原文地址:http://blog.csdn.net/kai8wei/article/details/46389961