467,917 Members | 1,324 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,917 developers. It's quick & easy.

oveloading operator<< and chaining

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
  • viewed: 2390
Share:
1 Reply
weaknessforcats
Expert Mod 8TB
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.

Similar topics

4 posts views Thread by franky.backeljauw | last post: by
3 posts views Thread by Victor Irzak | last post: by
3 posts views Thread by Alex Vinokur | last post: by
3 posts views Thread by Sensei | last post: by
14 posts views Thread by lutorm | last post: by
2 posts views Thread by Harry | last post: by
4 posts views Thread by Amadeus W. M. | last post: by
6 posts views Thread by johnmmcparland | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.