?
Solved

Overloading the insertion operator

Posted on 2005-03-23
10
Medium Priority
?
189 Views
Last Modified: 2012-05-05
Hello, I have found something strange.  I am using Visual C++ 6.0 SP6 so I am not sure if it is a bug with this or what but here goes.

I wrote a small piece of code:
#include <iostream>

int main()
{
      const char *pMessage = "Hello World\n";
      std::cout.operator<<(pMessage);  // Prints out the address of pMessage
      std::cout << pMessage;               // Prints out the message

      return 0;
}

When I trace through the code the first call using cout matches the signature as const void * and the second one matches to  basic_ostream<_E, _Tr>& _O, const _E *_X in this case: basic_ostream<char, char_traits<char>>& _O, const char *_X as far as I am aware.

What I don't understand is why operator<<() is using a different signature from plain << I thought they are synonymous.  Can anyone tell me why?  Is it a bug to do with Microsofts C++ compiler?

Thanks
Grant
0
Comment
Question by:Grant Rogers
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 5

Expert Comment

by:beryl666
ID: 13618648
yes. it return the refference of the pMessag only.
see:
http://www.edm2.com/0512/introcpp5.html
0
 
LVL 5

Accepted Solution

by:
beryl666 earned 380 total points
ID: 13618743
try this
operator<<(cout,pMessage);

then it will  Prints out the message
0
 
LVL 5

Expert Comment

by:beryl666
ID: 13618760
std::operator<<(cout,pMessage); //your style
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 5

Expert Comment

by:beryl666
ID: 13618784
std::cout.operator<<(pMessage);  // this is only for character
 std::cout.operator<<(*(pMessage));  // this u will get character H

//please remember that the style to overload operator are not the same
for example
you want to overload
a++;
++B;
//prefix and postfix it has different style
// if i am not mistaken one is ++ operator() and another is operator()++

hope this clear
0
 
LVL 5

Expert Comment

by:beryl666
ID: 13618797
sorry is operator++()
0
 
LVL 8

Expert Comment

by:novitiate
ID: 13619253
>>// if i am not mistaken one is ++ operator() and another is operator()++
A correction:

operator ++ () and operator ++ (int)

_novi_
0
 
LVL 5

Expert Comment

by:beryl666
ID: 13619275
ok thanks.
0
 
LVL 8

Assisted Solution

by:_corey_
_corey_ earned 120 total points
ID: 13619425
You aren't calling the same operator.

basic_ostream has operator<< overloads for type void* and other basic types specifically.

then it also has opeartor<< overloads for character output that is a 2 parameter overload where the first parameter is a basic_ostream reference.

You can view this in the ostream header or section 27.6.2.1 of the ISO 14882:2003 c++ specification.

corey
0
 

Author Comment

by:Grant Rogers
ID: 13622831
Hi thanks for the info.  I now understand why my call was producing the result it did and how to retify this.
0
 
LVL 5

Expert Comment

by:beryl666
ID: 13622929
welcome :-)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question