Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7067733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:14:16+00:00 2026-05-28T05:14:16+00:00

I have an overloaded << operator from my reckful class implemented as follows: ostream&

  • 0

I have an overloaded << operator from my reckful class implemented as follows:

ostream& operator << (ostream& os, const reckful& p)
{
    os << p.PrintStuff();

    return os;
}

PrintStuff() just being a member function of reckful that returns a string.

The way I understand things, if I were to write something like cout << reckobject << endl; in main(), cout << reckobject would take precedence and my overloaded << (using cout as the left operand and reckobject as the right operand) would return the ostream object os, leaving the expression os << endl; to be evaluated which would output the string and then end the line. So, the first << is the one I declared and the second is the standard << right?

However, my main question is… what is the sequence of events, which are the left and right operands, and which << operators are which when I run a statement like this:

cout << "reckful object = " << reckobject << "!" << endl;

Why does this work if there isn’t an ostream object and a reckful object on either side of one << ?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:14:17+00:00Added an answer on May 28, 2026 at 5:14 am

    If you notice standard way to implement << it returns the ostream itself. This is the critical piece

    So something like

     cout << "reckful object = " << reckobject << "!" << endl;
    

    will be called once for

    cout << "reckful object = " 
    

    This function call will return a ostream with which the second call will be made

    namely

       cout  << reckobject;
    

    so on an so forth.

    You can test is out by implementing your << as

    void operator << (ostream& os, const reckful& p)
    {  
        os << 1;
    }
    

    in which case you can do

    std::cout << p;
    

    but not

    std::cout << p << std::endl;
    

    The operators make it harder to understand but consider this Point class

    struct Point
    {
        Point& setX( int x) { X = x; return *this;}
        Point& setY( int y) { Y = y; return *this;}
    
        int X;
        int Y;
    };
    

    The way setX and setY are defined, allows

        Point p;
        p.setX( 2 ).setY( 4 );
    

    This is the same mechanism << is using to chain function calls.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class which does not have copy constructor or operator= overloaded. The
Hi I have a code like this, I think both the friend overloaded operator
Are there exceptions for types which can't have thier assignment operator overloaded? Specifically, I'm
I'm in the process of creating a double-linked list, and have overloaded the operator=
If I have have some overloaded ostream operators, defined for library local objects, is
Possible Duplicate: Overload ++ operator Say i have a class in which i overloads
I have a Card class and I want to overload the > operator to
Java's unary plus operator appears to have come over from C, via C++. int
I have an abstract base class called Shape from which both Circle and Rectangle
Suppose I have an abstract base class, that just defines a container on which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.