473,789 Members | 2,662 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Overloading operator>> question

Ook
I'm a but fuzzy about this. You would call it like this:Polynomial poly; //
Polynomial is my class with various properties
cin >> poly;

In the function itself, I would, for example, do the following:

istream& operator>>(istr eam& is, Polynomial& poly)
{
is >> poly._size;
is >> poly._coefficie nt[1];
return is;
}

Since we are passing poly by reference, I can modify it's properties
directly. This is where I loose it. If I'm modifying the properties of
poly directly in the overloaded function, why do I need to "return is"?
What exactly happens when you return from this function, and am I on the
right track about modifying the properties of poly in the function itself?
Oct 14 '05 #1
4 1559
"Ook" <Don't send me any freakin' spam> wrote in message
news:JK******** *************** *******@giganew s.com...
I'm a but fuzzy about this. You would call it like this:Polynomial poly;
// Polynomial is my class with various properties
cin >> poly;

In the function itself, I would, for example, do the following:

istream& operator>>(istr eam& is, Polynomial& poly)
{
is >> poly._size;
is >> poly._coefficie nt[1];
return is;
}

Since we are passing poly by reference, I can modify it's properties
directly. This is where I loose it. If I'm modifying the properties of
poly directly in the overloaded function, why do I need to "return is"?
You don't. But it's almost always recommended that you do, so that
the '>>' calls can be chained, e.g.

std::cin >> poly1 >> poly2;

... as we're accustomed to doing with e.g. 'int' objects.
What exactly happens when you return from this function,
Nothing 'happens', it's simply that a reference to the
stream is being returned. The caller is free to use it
or not. I'd always advise the caller does use it, if
for no other reason that to check for errors, e.g.

if(!(std::cin >> poly1))
std::cerr << "input error\n";
and am I on the
right track about modifying the properties of poly in the function itself?
Yes, that's normally the way it's done. But since typically those
'attributes', e.g. '_size' and '_coefficient' would be private,
common practice is to make your operator>> function a friend
of the class. Another way would be to make it not a friend,
and have it call a public function which would actually implement
the stream operation.

-Mike
Mike

Oct 14 '05 #2
Ook <Don't send me any freakin' spam> wrote:
I'm a but fuzzy about this. You would call it like this:Polynomial poly; //
Polynomial is my class with various properties
cin >> poly;

In the function itself, I would, for example, do the following:

istream& operator>>(istr eam& is, Polynomial& poly)
{
is >> poly._size;
is >> poly._coefficie nt[1];
return is;
}

Since we are passing poly by reference, I can modify it's properties
directly. This is where I loose it. If I'm modifying the properties of
poly directly in the overloaded function, why do I need to "return is"?
What exactly happens when you return from this function, and am I on the
right track about modifying the properties of poly in the function itself?


That sounds fine to me. The reason for returning "is" is so that you
can chain together >> commands:

std::ifstream in_file("whatev er.txt");
Polynomial p1;
Polynomial p2;
int i;

in_file >> p1 >> p2 >> i;

--
Marcus Kwok
Oct 14 '05 #3
Ook

"Mike Wahler" <mk******@mkwah ler.net> wrote in message
news:wy******** ******@newsread 2.news.pas.eart hlink.net...
<snip>

Yes, that's normally the way it's done. But since typically those
'attributes', e.g. '_size' and '_coefficient' would be private,
common practice is to make your operator>> function a friend
of the class. Another way would be to make it not a friend,
and have it call a public function which would actually implement
the stream operation.

-Mike
Mike


holy moly, 5 minutes and there is an answer here! I'm taking a Data
Structures class, and I gotta say, this NG is the best place to go when I'm
stumped and can't google/search the answer to a problem!

Actually, I do declare it as friend, and I'm glad I'm on the right track. I
can usually figure these things out, but I'm not always sure that the answer
I come up with is right, and getting the syntax just right is still hard for
me. Just out of curiosity, how would I go about using the reference to the
stream that is returned? I don't think I have ever done so because I do
everything I need to in the overloaded function itself. And tnx for the fast
response :)
Oct 14 '05 #4

"Ook" <Ook Don't send me any freakin' spam at zootal dot com delete the
Don't send me any freakin' spam> wrote in message
news:xZ******** ************@gi ganews.com...

"Mike Wahler" <mk******@mkwah ler.net> wrote in message
news:wy******** ******@newsread 2.news.pas.eart hlink.net...
<snip>

Yes, that's normally the way it's done. But since typically those
'attributes', e.g. '_size' and '_coefficient' would be private,
common practice is to make your operator>> function a friend
of the class. Another way would be to make it not a friend,
and have it call a public function which would actually implement
the stream operation.

-Mike
Mike
holy moly, 5 minutes and there is an answer here!


Many folks from all over the world ask and answer
questions here. It's just random chance that mine
was the first answer you saw. Also, don't take my
word as 'gospel', I'm human, and have been known
to make errors. :-) And even if my answer is perfectly
correct, it's always a good idea to read other replies
as well, to get different perspectives (and corrections
to those who err).
I'm taking a Data Structures class, and I gotta say, this NG is the best
place to go when I'm stumped and can't google/search the answer to a
problem!
Yes, I learn much here as well.
Actually, I do declare it as friend, and I'm glad I'm on the right track.
I can usually figure these things out, but I'm not always sure that the
answer I come up with is right, and getting the syntax just right is still
hard for me. Just out of curiosity, how would I go about using the
reference to the stream that is returned?

I already showed you:

if(!(std::cin >> poly1))
std::cerr << "input error\n";

The expression 'std::cin >> poly1' returns a reference
to 'std::cin'. It is 'used' here by evaluating its value
in a boolean context (with the ! operator).
I don't think I have ever done so because I do everything I need to in the
overloaded function itself.
Except the most important part: checking for errors. :-)
(If the operation had failed, the stream would go into
a 'fail' state, and *any* subsequent i/o requests would
also fail. Let this problem 'cascade' many levels of
function calls, and you'd have no idea where the problem
was.)

*Always* check for errors, *especially* with i/o, and
*more* especially when the input comes from a user.
And tnx for the fast response :)


You're welcome.

-Mike
Oct 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
1772
by: maths_fan | last post by:
Can't understand how to overload these operations and for what reason?
4
2483
by: hall | last post by:
Hi all. I have run into a problem of overloading a templatized operator>> by a specialized version of it. In short (complete code below), I have written a stream class, STR, which defines a templatized operator>>() as a member that can deal with the built in C types (int, char, float...) template <class tType> STR& STR::operator>>(tType & t); I then attempted to add an overloaded version of this to support my own
4
1879
by: Don Hedgpeth | last post by:
Here's a question - I'm new to c++ and I have two classes that overload the >> operator. One class calls the other...such as. //code for class1 friend std::istream& operator >> (std::istream& lhs, class1& rhs) { .....random code here return lhs:} //code for class2 private: class1 jimbo;
6
1923
by: n2xssvv g02gfr12930 | last post by:
Does anyone know of an example of overloading operator ->* Cheers JB
4
2626
by: jelle | last post by:
Hi, I use python quite a bit to couple different programs together. Doing so has been a _lot_ easier since subprocess came around, but would really like to be able to use the succinct shell syntax; >, <, | That really shouldn't be too hard to wrap in a class, but so far I didn't succeed to do so this well, since I'm facing some trouble with operator precedence that I do not know how to overcome.
11
2083
by: Ashwin | last post by:
hi guys, i have overloaded >operator for my own string class .the function is as given below istream& operator>>(istream &in,string1 &s) { char *a; a=new char; in>>a; s=a;
0
1108
by: rama270 | last post by:
Problem: The operator -> is overloaded thus Class * operator-> () { return this; } but this causes a problem for const member functions . The purpose of overloading is that both . and -> can be used . Is there a way out for const member function ?
2
2506
by: Wayne Marsh | last post by:
Hello, Is it considered sane/good practice to write a global operator for the insertion and extraction operators of an fstream in binary mode to serialize a binary class, or are they strictly meant for formatted text input and output? Let's imagine, for example, that I had a standard Windows BMP file (I am aware that C++ has no concept of a BMP - this is simply putting my question in a simple context). If I wanted to load it into a...
3
2686
by: Demoris | last post by:
I have a class that I believe should work. I've compared it to previous programs I've written, compared it to examples from my professor, to examples in my textbook, but can't see why I get the errors I get when building. All errors take place in the line friend istream& operator>>(istream& ins, QuadPolyn& P); Please note this is NOT an assignment, just an example of what we should be able to do. #ifndef quadpolyn_h #define...
0
9666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9511
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10139
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9983
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9020
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5417
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3700
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.