• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 190
  • Last Modified:

Overloading the insertion operator

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
Grant Rogers
Asked:
Grant Rogers
2 Solutions
 
beryl666Commented:
yes. it return the refference of the pMessag only.
see:
http://www.edm2.com/0512/introcpp5.html
0
 
beryl666Commented:
try this
operator<<(cout,pMessage);

then it will  Prints out the message
0
 
beryl666Commented:
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.

 
beryl666Commented:
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
 
beryl666Commented:
sorry is operator++()
0
 
novitiateCommented:
>>// if i am not mistaken one is ++ operator() and another is operator()++
A correction:

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

_novi_
0
 
beryl666Commented:
ok thanks.
0
 
_corey_Commented:
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
 
Grant RogersMonitoring ConsultantAuthor Commented:
Hi thanks for the info.  I now understand why my call was producing the result it did and how to retify this.
0
 
beryl666Commented:
welcome :-)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now