重载提取运算符>>在C ++中 [英] overloading the extraction operator >> in C++

查看:129
本文介绍了重载提取运算符>>在C ++中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能的重复:

操作员重载


我必须编写一个时钟程序, ,分钟和秒,同时重载提取算子。这些是我的代码:



clockType.h

 #include< iostream> 
using namespace std;

class clockType
{
public:
clockType();
void getTime();
friend istream& operator>>>(istream& const clockType);
private:
int hr,min,sec;
}

clockType.cpp

 #include< iostream> 
#include'clockType.h
using namespace std;

clockType :: clockType()
{
hr = 0;
min = 0;
sec = 0;
}

void clockType :: getTime()
{
while(hr> = 24)
hr = hr-24;
while(min> = 60)
min = min-60;
while(sec> = 60)
sec = sec-
cout << setfill('0')
<< setw(2)<< hr<<:
< ;< min<<:
<< setw(2)<< sec<< endl;
}

istream& operator> ;(istream& in,clockType cl)
{
in>> cl.hr> cl.min> cl.sec;
return in;
}

entryPoint.cpp

 #include< iostream> 
#include'clockType.h'

using namespace std;

int main()
{
clockType clock;
cout<<Enter hr,min,sec;
cin>> clock;
clock.getTime();
return 0;
}

没有错误。我的问题是,当我输入hr,min和sec,为什么它输出00:00:00?为什么>>不将其值传递给对象时钟?

解决方案

operator>> 需要

  istream& >>>(istream& in,clockType& cl)



您当前的代码接受 > clockType 实例,因此当调用操作符时,会生成 clock 的临时副本,并且操作员会这样做。该副本随后会被丢弃,您的原始时钟仍未修改。



此处的另一个问题是,如果您从输入流成功读取任何内容。对>> 操作中的任何和所有操作可能会失败,这可能会导致 cl 处于未知状态。所以首先你应该用 if(in>> cl.hr)测试成功。



这还不够,因为第一次读操作( hr )可能成功,但下一个操作可能失败;这将使您的 cl 处于未知状态。如果提取运算符具有事务语义,即或者更新所有三个成员,或者以其先前状态离开对象,这将是好的。一种方法是首先读入局部变量,并且只有当所有三个读取都成功时,将值复制到 cl 中。


Possible Duplicate:
Operator overloading

I have to code a clock program in which I could enter the hours, minutes and seconds while overloading the extraction operator. These are my codes:

clockType.h

#include<iostream>
using namespace std;

class clockType
{
public:
   clockType();
   void getTime();
   friend istream& operator>>(istream&, const clockType);
private:
   int hr, min, sec;
}

clockType.cpp

#include<iostream>
#include'clockType.h"
using namespace std;

clockType::clockType()
{
    hr = 0;
    min = 0;
    sec = 0;
}

void clockType::getTime()
{
    while(hr>=24)
        hr = hr - 24;
    while(min>=60)
        min = min - 60;
    while(sec>=60)
        sec = sec - 60;
    cout<<setfill('0')
        <<setw(2)<<hr<<":"
        <<setw(2)<<min<<":"
        <<setw(2)<<sec<<endl;
 }

 istream& operator>>(istream& in, clockType cl)
 {
    in>>cl.hr>>cl.min>>cl.sec;
    return in;
 }

entryPoint.cpp

 #include<iostream>
 #include'clockType.h'

 using namespace std;

 int main()
 {
   clockType clock;
   cout<<"Enter hr, min, sec";
   cin>>clock;
   clock.getTime();
   return 0;
 }

There is no error. My question is, as I enter the hr, min and sec, why does it output 00:00:00? Why doesn't the >> pass its values to the object clock?

解决方案

The signature of the operator>> needs to be

istream& operator>>(istream& in, clockType& cl) 

That is, it should accept a reference to a clockType instance.

Your current code accepts a clockType instance so when the operator is invoked a temporary copy of your clock is made and the operator works on that. The copy is then discarded, and your original clock remains unmodified.

Another issue here is that you are not checking if you successfully read anything from your input stream. Any and all of the >> operations on in could fail, which could leave cl in an unknown state. So first of all you should test for success with something like if(in >> cl.hr).

That's still not enough because the first read operation (into hr) could succeed, but the next one could fail; that will leave your cl in an unknown state. It would be good if the extraction operator had transaction semantics, i.e. either it updates all three members or otherwise it leaves the object in its previous state. One way to do that would be to read into local variables first, and only if all three reads succeed copy the values into cl.

这篇关于重载提取运算符&gt;&gt;在C ++中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆