Thread: Understanding overloading of << operator

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    29

    Understanding overloading of << operator

    Code:
    #include <iostream>
    using namespace std;
    
    
    int & fonksiyon(int&);
    class Date
    {
        int mo, da, yr;
    public:
        Date(int m, int d, int y)
        {
            mo = m; da = d; yr = y;
        }
        friend ostream& operator<<(ostream& os, const Date& dt);
    };
    
    
     ostream& operator<<(ostream& os, const Date& dt)
    {
        os << dt.mo << '/' << dt.da << '/' << dt.yr;
        return os;
    }
    
    
    int main()
    {
        Date dt(5, 6, 92);
        cout <<"Date:" << dt << ".";
    }

    I thought about this thing much,and searched for a good explanation on web too,but I couldn't find what I need,and I'm bored.


    The thing which I don't understand is:
    Code:
    ostream& operator<<(ostream& os, const Date& dt);

    ostream& os is a reference for cout I think ,operator << function takes cout as an argument I think.


    Then what does :
    Code:
    return os;

    do?
    Last edited by Awareness; 12-18-2014 at 07:30 PM.

  2. #2
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    I could be wrong, but I believe the purpose of returning the ostream& is so that it can be used in intermediate evaluations when you have many data types tied together with << operators.

    For example, try returning void. Chances are everything will print correctly so long as you are only printing one thing ( std::cout << X; ). When you need to print many things ( std::cout << X << "\n" << Y; ), you will need the operator to return the reference.
    WndProc = (2[b] || !(2[b])) ? SufferNobly : TakeArms;

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    29
    Quote Originally Posted by Alpo View Post
    I could be wrong, but I believe the purpose of returning the ostream& is so that it can be used in intermediate evaluations when you have many data types tied together with << operators.

    For example, try returning void. Chances are everything will print correctly so long as you are only printing one thing ( std::cout << X; ). When you need to print many things ( std::cout << X << "\n" << Y; ), you will need the operator to return the reference.

    Thanks for your answer.What does ostream& return actually?

    Code:
     cout  <<"Date:" << dt << "."; 


    Does it become like
    Code:
    cout << "Date:" << cout << "."
    when it returns?

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    ostream& os is a reference for cout I think ,operator << function takes cout as an argument I think.


    You may want to study this link.

    Jim

  5. #5
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    Quote Originally Posted by Awareness View Post
    Thanks for your answer.What does ostream& return actually?

    Code:
     cout  <<"Date:" << dt << "."; 


    Does it become like
    Code:
    cout << "Date:" << cout << "."
    when it returns?
    Taking an expression like
    Code:
     std::cout << A << B << C;
    I think you can look at it something like:

    1.
    Code:
    ( ( ( std::cout << A ) << B ) << C ); // If you look at the () like a function
    2.
    Code:
    ( ( std::cout << B ) << C ); // The std::cout you see here would be the return of #1
    3.
    Code:
    ( std::cout << C );
    WndProc = (2[b] || !(2[b])) ? SufferNobly : TakeArms;

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by jimblumberg View Post
    You may want to study this link.

    Jim
    Your link is broken.

    Anyway, the expression (a << b) is translated to operator << (a, b) or a.operator << (b) by the compiler. Then it just looks for an overload as usual.
    So the function says, give me anything where a is of type std::ofstream or any of its derived types and where b is a Date or any of its derived classes.
    std::ostream is the base class for all output streams. It can be a file stream, a string stream, or yes, even a stream to the console or anything else than derived from std::ostream. In your case, "a" is std::cout and "b" is dt. Make sense?

    About the return: Alpo is correct.
    If you write

    std::cout << A << B << C;

    It is interpreted by the compiler as

    ( ( ( std::cout << A ) << B ) << C ); // If you look at the () like a function

    So we basically call operator << for the return of the express (std::cout << A). For that reason, we tend to return the stream itself. So if we call operator << with std::cout, then we return std::cout so it becomes

    ( ( std::cout << B ) << C ); // If you look at the () like a function

    Of course, [I]this works with any type of stream,[/I] not just std::cout.
    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.

  7. #7
    Registered User
    Join Date
    Feb 2012
    Posts
    29
    Thanks for your answers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New to C Programming- Need help understanding !! operator
    By shellwoo3 in forum C Programming
    Replies: 4
    Last Post: 07-17-2012, 07:18 PM
  2. Operator Overloading
    By xiaolim in forum C++ Programming
    Replies: 3
    Last Post: 10-25-2009, 03:51 AM
  3. Help understanding the modulus operator
    By matrixx333 in forum C Programming
    Replies: 5
    Last Post: 10-01-2009, 06:06 AM
  4. overloading operator help
    By syrel in forum C++ Programming
    Replies: 8
    Last Post: 09-30-2004, 10:49 AM
  5. Help understanding conditional operator
    By Sereby in forum C Programming
    Replies: 7
    Last Post: 08-09-2004, 12:24 PM