By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,820 Members | 1,142 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,820 IT Pros & Developers. It's quick & easy.

cout2,

P: n/a
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;
}

Nov 7 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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::operator<< (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
Nov 7 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.