By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,238 Members | 1,804 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,238 IT Pros & Developers. It's quick & easy.

oveloading operator<< and chaining

P: 1
Hi,

I have a question about overloading operator<< . Actually I am trying to understand how it works when chaining multiple calls to this operator. I have a very simple class (MyOut) with an overloaded operator<<, which takes a string as input parameter and returns a reference to an ostream object (std::cout in this case).
In the main file I instantiate an object of MyOut and then write a message to console in two different ways:
1. chaining several strings
2. sending individually each string

What I don't understand is why in the first case (when chaining) the overloaded operator is called only one time, for the first string? What happens for the other two strings ?!? How actually the chaining works ?
What I am trying to do is to buffer the input string "str", doing something like that:
m_str += str; (instead of the printf call)
where m_str is std::string as well and is cleared somewhere else. Which works fine only when not chaining strings.
I would very much appreciate if you have any answer to my problem.

Cheers,
buburuz

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. class MyOut {
  7.    ostream& output;
  8.  
  9.    public:
  10.  
  11.      MyOut()
  12.      : output( std::cout ) {}
  13.      ~MyOut() {}
  14.  
  15.    std::ostream& operator<<(const std::string& str)
  16.    {
  17.       printf ("+++ called operator<<\n");
  18.       return output << str;
  19.    }
  20. };
  21.  
  22.  
  23. int main () 
  24. {
  25.    MyOut p;
  26.  
  27.    // chaining
  28.    p << "This is " << "just a " << "test!\n" << std::endl;
  29.  
  30.    // individual calls
  31.    p << "This is ";
  32.    p << "just a ";
  33.    p << "test!" << std::endl;
  34.  
  35.  return 0;
  36. }
Jun 3 '07 #1
Share this Question
Share on Google+
1 Reply


weaknessforcats
Expert Mod 5K+
P: 9,197
Yes, C++ does make the magic. This code:

p << "This is " << "just a " << "test!\n" << std::endl;
is really 4 calls to:

Expand|Select|Wrap|Line Numbers
  1. ostream& operator<<(Myout*, char*);   //this one is yours. You see your display.
  2. ostream& operator<<(ostream&, char*);  //this is not yours
  3. ostream& operator<<(ostream&, char*);  //this one is not yours
  4. ostream& operator<<( ostream& (*fp)(ostream&)); //not yours either
  5.  
Total calls to your operator<< : 1.

You may say that this is not yours:

ostream& operator<<(Myout*, char*);

but since it is a member function, the this pointer is supplied by the compiler as a hidden first argument.
Jun 3 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.