Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > cout2,

Reply
Thread Tools

cout2,

 
 
vsgdp
Guest
Posts: n/a
 
      11-07-2006
Why does the first statement output nonsense, but not the second. That
is, why aren't the statements equivelement?

#include<iostream>
#include <cmath>
using namespace std;

int main()
{
cout.operator <<("sin(2)=").operator <<(sin(2)).operator <<(endl);
cout << "sin(2)=" << sin(2) << endl;
}

 
Reply With Quote
 
 
 
 
Stuart Redmann
Guest
Posts: n/a
 
      11-07-2006
vsgdp wrote:
> Why does the first statement output nonsense, but not the second. That
> is, why aren't the statements equivelement?
>
> #include<iostream>
> #include <cmath>
> using namespace std;
>
> int main()
> {
> cout.operator <<("sin(2)=").operator <<(sin(2)).operator <<(endl);


cout.operator << "sin(2)="; invokes the member function
ostream:perator<< (const void* param)
and not what you expected:
inline ostream operator<< (ostream& _O, const char *param)
The reason for this is that you explicitely ask for the member function
operator<<, but operator<< for char* is defined as static function (as
most other custom defined stream operators are). What you are seeing is
the address of the string.

> cout << "sin(2)=" << sin(2) << endl;
> }


You should always stick to this invokation scheme.

Regards,
Stuart
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments