473,396 Members | 2,059 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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
1 2677
weaknessforcats
9,208 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

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

Similar topics

4
by: franky.backeljauw | last post by:
Hello, I have a problem with using a copy constructor to convert an object of a templated class to object of another templated class. Let me first include the code (my question is below): ...
3
by: Victor Irzak | last post by:
Hello, I have an ABC. it supports: ostream & operator << I also have a derived class that supports this operator. How can I call operator << of the base class for derived object??? Is it...
3
by: Alex Vinokur | last post by:
Member operators operator>>() and operator<<() in a program below work fine, but look strange. Is it possible to define member operators operator>>() and operator<<() that work fine and look...
3
by: Sensei | last post by:
Hi. I have a problem with a C++ code I can't resolve, or better, I can't see what the problem should be! Here's an excerpt of the incriminated code: === bspalgo.cpp // THAT'S THE BAD...
14
by: lutorm | last post by:
Hi everyone, I'm trying to use istream_iterators to read a file consisting of pairs of numbers. To do this, I wrote the following: #include <fstream> #include <vector> #include <iterator> ...
4
by: bluekite2000 | last post by:
Here A is an instantiation of class Matrix. This means whenever user writes Matrix<float> A=rand<float>(3,2);//create a float matrix of size 3x2 //and fills it up w/ random value cout<<A; the...
2
by: Harry | last post by:
Hi all, I am writing a logger program which can take any datatype. namespace recordLog { enum Debug_Level {low, midium, high}; class L { std::ofstream os; Debug_Level cdl; const...
4
by: Amadeus W. M. | last post by:
What is the difference between friend ostream & operator<<(ostream & OUT, const Foo & f){ // output f return OUT; } and template <class X>
6
by: johnmmcparland | last post by:
Hi all, when I write a subclass, can I inherit its superclass' << operator? As an example say we have two classes, Person and Employee. class Person has the << operator; /**
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.