Thread: Overloading <<

  1. #1
    Registered User
    Join Date
    Nov 2018

    Overloading <<

    Hi all,
    i am studying a C++ book. Now i am having the chapter slowly going to classes. The book is ok, makes, but makes me angry from time to time, because the guy mention sth new but used a complicated example...i need help to understand this example :

    struct Person {
        string name_;
        int age_;
        string place_;
    // i don't get this implementation
    std::ostream& operator<<(std::ostream& os, Person p) {
            return os << p.name_ << " ("<< p.age_<<") from " << p.place_;
    int main() {
    Person paul {"Paul", 23, "Dresden"};
    cout << "You are " << paul << ", right ?\n";
    what i dont understand is the overloading of <<. especially the parameter list (std::ostream& os, Person p).

    in the main.. the usage happens with "<< paul"
    where do i see here the parameters , especially std::ostream& os

    so i dont really understand neither the implementation nor the usage of it.

    would be grateful if someone can explain me this in an understandable way.

    i have watched a video about overloading the + operator, and it was easy to understand.

    but introducing the overloading in a beginner book with such an example is really mean...there is not even a proper explanation..

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Let's ignore the operator chaining and just consider:
    cout << paul;
    This is equivalent to:
    operator<<(cout, paul);
    Now you can see the arguments.
    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

  3. #3
    Registered User
    Join Date
    Aug 2019
    inside a singularity
    When I was learning basics about operator overloading, I came across this and it's really good.

    Operator Overloading in C++ - GeeksforGeeks

    Stroustrup: C++ Style and Technique FAQ

    Operator overloading - Wikipedia

  4. #4
    Registered User
    Join Date
    Nov 2018
    Not easy to see that as a beginner...thanks Laserlight--

    @Zeus_ : i have good experience with geeksforgeeks, will take a look at it, thanks a lot
    Last edited by Scatman; 08-18-2019 at 08:27 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overloading in C++
    By jdbrande in forum C++ Programming
    Replies: 4
    Last Post: 04-24-2009, 02:53 PM
  2. overloading []
    By pktcperlc++java in forum C++ Programming
    Replies: 3
    Last Post: 03-02-2005, 09:09 PM
  3. overloading =
    By Mr_Jack in forum C++ Programming
    Replies: 5
    Last Post: 12-23-2003, 01:45 PM
  4. So, no overloading [][]?
    By kooma in forum C++ Programming
    Replies: 3
    Last Post: 03-13-2002, 09:48 AM
  5. overloading >
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 03-04-2002, 07:16 AM

Tags for this Thread