首页 > 其他 > 详细

重载>>运算符

时间:2017-09-20 16:11:36      阅读:225      评论:0      收藏:0      [点我收藏+]
#include <iostream>  
#include <stdio.h>  
using namespace std;  
class Input  
{  
    public:  
       //实际重载是右移运算符  
       Input & operator >> (int &a)  
       {  
           scanf("%d",&a);  
           fflush(stdin);//去掉回车  
           return *this;  
       }  
       Input & operator >> (float &a)  
       {  
           scanf("%f",&a);  
           fflush(stdin);//去掉回车  
           return *this;  
       }  
       //重载输出运算符  
      friend ostream & operator <<(ostream &os,Input &v);  
};  
ostream & operator <<(ostream &os,Input & v)  
{  
     os << v.m_pData->id ;  
     return os;  
}  
  
Input In;  
int main(int argc, char* argv[])  
{  
    int   a;  
    float b;  
    In >>a>>b;  
    cout << a << "," << b <<endl;  
    getchar();  
}


重载>>运算符

原文:http://9233403.blog.51cto.com/9223403/1967106

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!