重载一些常见的运算符
2015-08-09 11:53
260 查看
一些常见的运算符重载
++自增运算符在C或C++中既可以放在操作数之前,也可以放在操作数之后,但是前置和后置的作用又是完全不同的那么要怎么重载它们,才可以有效的区分开来呢?今天我首先来说说C++中是怎么处理前置运算符和后置运算符的重载的。
在C++里编译器是根据运算符重载函数参数表里是否插入关键字int来区分前置还是后置运算。
插入运算符"<<"是双目运算符,左操作数为输出流类ostream的对象,右操作数为系统预定义的基本类型数据。头文件iostrem对其重载的函数原型为ostream& operator<<(ostream& ,类型名);类型名就是指基本类型数据。但如果要输出用户自定义的类型数据的话,就需要重载操作符"<<",因为该操作符的左操作数一定为ostream类的对象,所以插入运算符"<<"只能是类的友元函数或普通函数,不能是其他类的成员函数。一般定义格式:
ostream& operator<<(ostream& ,自定义类名&);
提取运算符">>"也是如此,左操作数为istream类的对象,右操作数为基本类型数据。头文件iostrem对其重载的函数原型为istream& operator>>(istream& ,类型名);提取运算符也不能作为其他类的成员函数,可以是友元函数或普通函数。它的一般定义格式为:
istream& operator>>(istream& ,自定义类名&);
++自增运算符在C或C++中既可以放在操作数之前,也可以放在操作数之后,但是前置和后置的作用又是完全不同的那么要怎么重载它们,才可以有效的区分开来呢?今天我首先来说说C++中是怎么处理前置运算符和后置运算符的重载的。
在C++里编译器是根据运算符重载函数参数表里是否插入关键字int来区分前置还是后置运算。
<span style="font-size:18px;">class Point//三维坐标 { private: int x; int y; int z; public: Point(int x=0,int y=0,int z=0) { this->x =x; this->y = y; this->z = z; } void operator++();//成员函数重载前置运算符++ Point operator++(int);//成员函数重载后置运算符++ friend Point operator++(Point& point);//友元函数重载前置运算符++ friend Point operator++(Point& point,int);//友元函数重载后置运算符++ void showPoint(); }; void Point::operator++() { ++this->x; ++this->y; ++this->z; //return*this;//返回自增后的对象 } Point Point::operator++(int) { Point point(*this);//这样可以对一个对象初始化 this->x++; this->y++; this->z++; return point;//返回自增前的对象 } Point operator++(Point& point) { ++point.x; ++point.y; ++point.z; return point;//返回自增后的对象 } // Point operator++(Point& point,int) { Point point1(point); point.x++; point.y++; point.z++; return point1;//返回自增前的对象 } void Point::showPoint() { std::cout<<"("<<x<<","<<y<<","<<z<<")"<<std::endl; } int main() { Point point(1,1,1); point++;//++point 这两种调用都可以 point.showPoint();//前置++运算结果 point = point++; //后置只能这样调用,不能++point point.showPoint();//后置++运算结果 point++;//++point point.showPoint();//前置++运算结果 point = point++; point.showPoint();//后置++运算结果 return 0; }</span>注释:直接把上面的代码复制到vs2010中进行运行是错误的。文中定义了多种“++"运算符重载函数,调用的时候会都是point++或者是++point,这样会出现++操作数不知道和那个函数匹配。后置运算符重载函数比前置运算符重载函数多了一个int类型的参数,这个参数只是为了区别前置和后置运算符,此外没有任何作用。所以在调用后置运算符重载函数时,int类型的实参可以取任意值。
插入运算符"<<"是双目运算符,左操作数为输出流类ostream的对象,右操作数为系统预定义的基本类型数据。头文件iostrem对其重载的函数原型为ostream& operator<<(ostream& ,类型名);类型名就是指基本类型数据。但如果要输出用户自定义的类型数据的话,就需要重载操作符"<<",因为该操作符的左操作数一定为ostream类的对象,所以插入运算符"<<"只能是类的友元函数或普通函数,不能是其他类的成员函数。一般定义格式:
ostream& operator<<(ostream& ,自定义类名&);
提取运算符">>"也是如此,左操作数为istream类的对象,右操作数为基本类型数据。头文件iostrem对其重载的函数原型为istream& operator>>(istream& ,类型名);提取运算符也不能作为其他类的成员函数,可以是友元函数或普通函数。它的一般定义格式为:
istream& operator>>(istream& ,自定义类名&);
class Complex //复数类 { private://私有 double real;//实数 double imag;//虚数 public: Complex(double real=0,double imag=0) { this->real=real; this->imag=imag; } friend ostream& operator<<(ostream& o,Complex& com);//友元函数重载提取运算符"<<" friend istream& operator>>(istream& i,Complex& com);//友元函数重载插入运算符">>" }; ostream& operator<<(ostream& o,Complex& com)//返回类型可以为void { cout<<"输入的复数:"; o<<com.real; if(com.imag>0) o<<"+"; if(com.imag!=0)o<<com.imag<<"i"<<std::endl; return o; } istream& operator>>(istream& i,Complex& com)//返回类型可以为void { cout<<"请输入一个复数:"<<endl; cout<<"real(实数):"; i>>com.real; cout<<"imag(虚数):"; i>>com.imag; return i; } int main() { Complex com; cin>>com; cout<<com; return 0; }
相关文章推荐
- HDU 1466 直线交点情况
- shell的定义
- scala中下划线的几种用法
- Caffe上训练使用自己的数据
- Spring定时任务配置
- HTTP学习
- Insertion Sort List
- 2015自考数据结构导论——宏观篇
- Android AIDL线程通信之Service回调客户端Activity
- 1083. List Grades (25)
- 2015年第二学期的计划
- bzoj1188: [HNOI2007]分裂游戏
- 关于界面滚动条的问题
- java并发(四) CopyOnWrite容器
- Stream的Dropzonejs样式演示
- 谈谈java中成员变量与成员方法继承的问题
- java TCP/IP实现简单的多人聊天功能
- 小心“好主意”
- Proguard.cfg 配置
- 【Java多线程】之六:Synchronization and Thread Safety