C++重载operator的示例[转]

2009-06-15 11:26转自:
http://hi.baidu.com/wonder0016/blog/item/2afe34386547b1c4d562258b.html

以下示例中定义了一个class test, 重载了<, +, +=, =, ==, <<, >>等符号:

#include<iostream>
#include<vector>
using namespace std;

class test{
public:
     int v;
   /*构造函数*/
     test():v(0){}
     test(const int &a):v(a){}
     test(const test &t1):v(t1.v){}
   
   /*以下重载小于号 < */
     //比较两个对象的大小
     bool operator<(const test &t1) const{
         return (v < t1.v);
     }
     //比较对象和int的大小
     bool operator<(const int &t1) const{
         return (v < t1);
     }
     //友元函数,比较int和对象的大小
     friend inline bool operator<(const int &a, const test & t1){
         return (a < t1.v);
     }
   
   /*以下重载赋值号 = */
     //对象间赋值
     test & operator=(const test &t1){
         v = t1.v;
         return *this;
     }
     //int赋值给对象
     test & operator=(const int &t1){
         v = t1;
         return *this;
     }
   
   /*以下重载加号 + */
     //对象加上 int
     test operator+(const int & a){
         test t1;
         t1.v = v + a;
         return t1;
     }
     //对象加对象
     test operator+(test &t1){
         test t2;
         t2.v = v + t1.v;
         return t2;
     }
   
   /*以下重载加等号 += */ 
     //对象加上对象
     test &operator+=(const test &t1){
         v += t1.v;
         return *this;
     } 
     //对象加上int
     test &operator+=(const int &a){
         v += a;
         return *this;
     }

   /*以下重载双等号 == */ 
     //对象==对象
     bool operator==(const test &t1)const{
         return (v == t1.v);
     } 
     //对象==int
     bool operator==(const int &t1)const{
         return (v == t1);
     } 
   
   /*以下重载 输入>> 输出<< */
     /*友元函数,输出对象*/
     friend inline ostream & operator << (ostream & os, test &t1){
         cout << "class t(" << t1.v << ")" << endl;
         return os;
     }
     /*友元函数,输入对象*/
     friend inline istream & operator >> (istream & is, test &t1){
         cin >> t1.v;
         return is;
     }
};

int main(){
     test t0, t1(3);
     test t2(t1);
     cout << t0 << t1 << t2;
     cin >> t1;
     t2 = t1;
     t2 += t1;
     t1 += 10;
     cout << t2;
     if(t1 < t2) cout << "t1 < t2";
     else if(t1 == t2) cout << "t1 = t2";
     else /* t1 > t2*/ cout << "t1 > t2";
     cout <<endl;
     system("pause");
     return 0;
}
在Linux下G++ 使用的是ISO C++这与我们在网上找到的很多基于Windows VC的C++在operator操作符重载上有一定不同,通过这几天自己摸索写下Linux ISO C++ operator+操作符重载实现.

在这里我提供了一个实例如下:
    1.我们需要实现类似于:Class A = Class B + int;
                         Class A = Class B + Class C;
                         Class A = int + Class B;这样的功能.
    2.第一和第二种情况比较容易实现,方法和VC下基本一致,可以通过下面的方式实现:
      <类> operator+ (const int) const;
      <类> operator+ (cosnt 类&) const;
    3.第三类情况实现和VC++ 有区别,我是通过下面的方法实现的:
      friend <类> operator+ (const int,const <类>&);
注意这里friend后的类只能使用传值的方式,不能使用传址或引用的方式,否则会出现参数个数使能是0个或1个的错误.
      在写函数实现部分时需写成
      <类> operator+ (const int value_left,const <类>& value_right){ ... }
      否则会提示这个实现的函数没有在<类>中声明.
      通过这样的重载我们就可以实现第三种情况了,这种情况一样适用于其他双目运算符重载.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值