Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

HeadInjury11

C++: overloading >>

This topic is 5691 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

does anyone know how to overload the >> for an input stream. what I have so far is: friend istream& operator>>(istream& istr, complex& x); ie. the class is complex (for complex numbers) is this right?

Share this post


Link to post
Share on other sites
Advertisement
Just so you know, the standard library already includes a complex class and iostream operators for it.

Share this post


Link to post
Share on other sites
I copied this out of somewhere. It''s an example of how to do it:

istream& operator>>(istream& s, complex& a) {
// possible input type:
// f
// (f)
// (f, f)
double re = 0, im = 0;
char c = 0;

s >> c;
if (c == ''('') {
s >> re >> c;
if (c == '','') s >> im >> c;
if (c != '')'') s.clear(ios_base::badbit); //set state
} else {
s.putback(c);
s >> re;
}

if (s) a = complex(re, im);
return s;
}

Since the variable names are so undescribing, I think I copied it from Stroustrup''s book.

Share this post


Link to post
Share on other sites
It''s not very good for different locales as you could end up with (1,2,4,6) for countries which use a comma to indicate a decimal place. Admittedly that is a short coming of the std::comple class too...

Share this post


Link to post
Share on other sites
quote:
Original post by petewood
It''s not very good for different locales as you could end up with (1,2,4,6) for countries which use a comma to indicate a decimal place.
Actually such countries (like mine) use ; to clarify it. (1,2,4) would be ambigious. (1,2;4) is not.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!