Solved

cout/cin overload

Posted on 2001-08-08
8
1,984 Views
Last Modified: 2010-08-05
Is it possible to overload cout & cin ?
if so, how? if not, why ?
0
Comment
Question by:ls1429
8 Comments
 
LVL 4

Expert Comment

by:IainHere
ID: 6363392
Yes, they're overloadable - that's pretty much the reason they were developed.  Don't forget that cout is defined as

extern ostream cout;

say you have your own class:

class WriteMe{
public:
    double m_writethis;
};

you would overload the << operator to work with cout like this:

ostream& operator<< (ostream&s, const WriteMe& write)
{
   return s << "your formatting here " << write.m_writethis;
}

[Ignoring the publicness of the member variable and the uselessness of the class]
0
 
LVL 4

Expert Comment

by:IainHere
ID: 6363469
Oh, and the cin being istream can be overloaded in the same fashion (of course, the class you're overloading for cannot be const in this case, and you must remove any formatting you define in the ostream - I've ignored this removal because it would complicate matters, and I'd probably get it wrong anyway :)

istream& operator>> (istream& s, WriteMe& write)
{
   s >> write.m_writethis;
   return s;
}

Of course in a real case, you'd have to put all sorts of error checking in there too.

you would use this exactly as you would a built in type:

WriteMe   bob;
bob.m_writethis = 77;
cout << bob;

...

WriteMe   bill;
cin >> bill;  //ignoring the formatting again.
0
 

Author Comment

by:ls1429
ID: 6363611
The above code ( tested for cout )did not got compiled.
But when I changed the overload function to friend function it got magically compiled.
I dont know why I got problems and by friend function how it solved/ redirected the purpose.

Can you explain ?
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 1

Expert Comment

by:glebspy
ID: 6363627
I just wanted to make a technical point.

cin and cout are objects. It's not possible to overload them, and the phrase doesn't even mean anything. It's only possible to overload functions (of which operators like << are a special case).

when you employ Iainhere's solution (he correctly guessed what you meant), you are using a different(overloaded) operator<<, but the same cin and cout.

It's possible to over*ride* cin and cout by saying something silly like

float cin;

or

string cout;

after which operator<< doesn't work any more..
0
 
LVL 4

Expert Comment

by:IainHere
ID: 6363930
>>But when I changed the overload function to friend function it got magically compiled.

Could you post the code - I don't understand.  In the meantime, here's the code for a functioning example (I just tested it).

#include <iostream>
using namespace std;

class WriteMe{
public:
   double m_writethis;
};

ostream& operator<< (ostream&s, const WriteMe& write)
{
  return s << "your formatting here " << write.m_writethis;
}

int main(int, char**)

{
     WriteMe jeff;
     jeff.m_writethis = 33;
     cout << jeff;    
     return 0;
}
0
 
LVL 1

Accepted Solution

by:
glebspy earned 25 total points
ID: 6364075
>The above code ( tested for cout )did not got compiled.
>But when I changed the overload function to friend function it got magically compiled.
>I dont know why I got problems and by friend function how it solved/ redirected the purpose.

>Can you explain ?


I've  encountered exactly this situation many times, and the right thing to do is to make the overloaded operator a friend function.

The reason is that the overloaded >> and << are not members of Iainhere's class Writeme! They are global. You can't make them member functions of Writeme. Imagine you had

class Writeme{
 double m_writethis;
 ostream&operator<<(ostream&a){return a<<m_writethis}
}bill;
that would work but you'd have to say
bill<<cout;
rather than cout<<bill; which is what I'd consider desirable.

So if you try to access

istream& operator>> (istream& s, WriteMe& write)
           {
             s >> write.m_writethis;
             return s;
           }

*operator>> has to have access to the member m_writethis*
so either m_writethis has to be declared public: (remember
members of a class are private by default) or the global operator>> has to be declared a friend. I personally use the friend way of doing things since I might want to write out all sorts of members of the class in the cout<< call, some of which I might think were more private than others.

I usually call writing functions which only have access to public members `display' or `show' rather than using operator<<, and then these are not friends and so they can only write out the public members.
0
 
LVL 11

Expert Comment

by:griessh
ID: 6851036
I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. Unless there is objection or further activity,  I will suggest to accept "glebspy, IanHere" comment(s) as an answer.

If you think your question was not answered at all, you can post a request in Community support (please include this link) to refund your points.
The link to the Community Support area is: http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
0
 
LVL 5

Expert Comment

by:Netminder
ID: 6874840
Per recommendation comment force/accepted by

Netminder
Community Support Moderator
Experts Exchange

IainHere: points for you at http://experts-exchange.com/jsp/qShow.jsp?ta=cplusprog&qid=20278011
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question