Thread: overloading <<

  1. #1
    Registered User
    Join Date
    May 2009

    overloading <<

    What's unclear to me here is what it actually is for a function to return a reference variable, and Prata (p. 520) emphasizes that you need to do that if you want your overloaded << to work properly.

    He uses the example of an object of the class Time, which has as member variables integers for hours and minutes. So, the definition of the overloaded << looks like this:

    ostream & operator<<(ostream & os, const Time &t)
       os << t.hours << " hours, " << t.minutes << " minutes";
       return os;
    It's clear to me why the function needs to return an ostream object, namely to get a line like cout << trip << " on Tuesday.\n"; to work properly (where trip is a variable holding a Time object).

    But why wouldn't it work to make the definition read simply:

    ostream operator<<(ostream & os, const Time & t)
    That's where my understanding is breaking down. It seems to me that the latter definition should also return cout, just not cout as reference object.

    I guess one reason for the necessity of the & might be that there can only be ONE cout object (just as Jet Li wanted to achieve!) and that without the & the compiler would try unsuccessfully to create another. So, when we try to get outside the function and go beyond the return os; line, without the &, the compiler tries to make a copy of cout to use as value outside the function. But it can't do it since there can only be 1 cout. Hence, maybe not even compiler error when you don't use the & but definitely run-time error when you try to make use of the function without the & (?).

    Am I on the right track in attempting to understand this?

    P.S.: I guess this question has a lot to do with understanding what cout actually is. I'm guessing that it's essentially a built-in variable representing the content of THE location in memory which the OS defines as holding screen contents for the current location. That understanding could no doubt also use a good deal of elaboration for the whole dynamic process but is at least the way I kind of picture the basics of it.

  2. #2
    The larch
    Join Date
    May 2006
    streams are not copyable, hence they cannot be passed or returned by value (what would it mean to suddenly have copies of cout?)
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Quote Originally Posted by Aisthesis
    It seems to me that the latter definition should also return cout, just not cout as reference object.
    It would return a copy of std::cout, but std::ostream objects, including std::cout, are not copyable. So in a way you are right to say that "the compiler would try unsuccessfully to create another".

    Even if the object was copyable, if you do not need a copy, it would make sense to return a reference anyway.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Join Date
    Oct 2007
    Inside my computer
    It would not make sense to return a copy anyway, because when you write
    cout << x << y << z
    You expect to write x, y and z to the cout object you specified here, not a copy of it. It would lead to subtle bugs down the road if you returned a copy.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. overloading <<
    By msshapira in forum C++ Programming
    Replies: 2
    Last Post: 05-06-2009, 02:11 PM
  2. Overloading fstream's << and >> operators
    By VirtualAce in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2007, 03:17 AM
  3. Overloading the << operator.
    By antex in forum C++ Programming
    Replies: 5
    Last Post: 05-31-2005, 01:37 AM
  4. overloading the output operator <<
    By neandrake in forum C++ Programming
    Replies: 9
    Last Post: 12-07-2004, 02:25 PM
  5. Replies: 8
    Last Post: 11-27-2004, 03:12 AM