Topic: Setting fail state in istream::operator>>()


Author: arnstein@netcom.com (David Arnstein)
Date: Wed, 11 May 1994 19:03:48 GMT
Raw View
I find it impossible to create an extractor (operator>>) to work with a data
type that I define myself.  Specifically, after I program a
     class Number;
(for example) I would like to enable reading an instance of Number using the
familiar extractor syntax, ie
     Number num;
     cin >> num;

My problem is that inside my extractor operator
     istream& operator>> (istream& ist, Number& num);
I would like to call
     ist.setstate(ios::failbit);
when I encounter garbage reading stream ist.  Guess what.  ios::setstate() is
protected.  So I can't call it!

I propose that a public method ios::setfail(void) be added to class ios which
simply calls ios::setstate(ios::failbit).

I find that the iostream package is moderately awkward to use and extremely
difficult to extend.  Since C++ is supposed to be object oriented, I'm very
disappointed.  I hope that the ANSI committee will consider standardizing the
iostream library, and making it easier to extend.
--
David Arnstein       |          What do you mean, "get a life"?
arnstein@netcom.com  |          This *is* my life!




Author: terry@uivlsisd.csl.uiuc.edu (Terry Lee)
Date: 11 May 94 22:47:51 GMT
Raw View
arnstein@netcom.com (David Arnstein) writes:

>My problem is that inside my extractor operator
>     istream& operator>> (istream& ist, Number& num);
>I would like to call
>     ist.setstate(ios::failbit);
>when I encounter garbage reading stream ist.  Guess what.  ios::setstate() is
>protected.  So I can't call it!

 Try "ist.clear(ios::failbit | ist.rdstate());".  This sets failbit and
doesn't disturb the other bits.  I think the naming of ios::clear() is a little
misleading.

--
Terry Lee (terry@uivlsi.csl.uiuc.edu)
Coordinated Science Lab
University of Illinois