cout2,

V

vsgdp

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;
}
 
S

Stuart Redmann

vsgdp said:
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::eek: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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top