About cout

T

tuyongce

Source code:
#include <iostream>
using namespace std;

int main()
{
cout.operator<<("test string");

return 0;
}

The output as follow:
0046B01C ----> why it's an address?

My question:
1) Why the output was an address and why not "test string" as expected
?
2) What's the difference between
cout<<"test";
and
cout.operator<<("test");
?
(I had thought they were the same! )
 
B

benben

Source code:
#include <iostream>
using namespace std;

int main()
{
cout.operator<<("test string");

return 0;
}

The output as follow:
0046B01C ----> why it's an address?

My question:
1) Why the output was an address and why not "test string" as expected
?
2) What's the difference between
cout<<"test";

The equivalent of the above should be:

std::eek:perator<< (std::cout, "test");
and
cout.operator<<("test");

This only dumps an address.
 
E

eriwik

Source code:
#include <iostream>
using namespace std;

int main()
{
cout.operator<<("test string");

return 0;

}The output as follow:
0046B01C ----> why it's an address?

My question:
1) Why the output was an address and why not "test string" as expected
?
2) What's the difference between
cout<<"test";

This calls this function:
basic_ostream<charT, traits>&
operator<<(basic_ostream<charT, traits>& os , const
basic_string said:
and
cout.operator<<("test");

This (I think) calls this function:
basic_ostream<charT,traits>&
operator<<(basic_streambuf<char_type,traits>* sb );
 
R

Ron Natalie

My question:
1) Why the output was an address and why not "test string" as expected
?
2) What's the difference between
cout<<"test";
and
cout.operator<<("test");
?
(I had thought they were the same! )

operator<< doesn't necessarily have to be implemented as a member
function.

By using the explicit call to the member function, you won't
match any of the nonmember function overloads.
 
R

Rolf Magnus

Source code:
#include <iostream>
using namespace std;

int main()
{
cout.operator<<("test string");

return 0;
}

The output as follow:
0046B01C ----> why it's an address?

My question:
1) Why the output was an address and why not "test string" as expected
?

There is no perfectly fitting operator in the class, so the type is using
the conversion that fits best.
2) What's the difference between
cout<<"test";

Here, the compiler can chosse between member an non-member operators.
and
cout.operator<<("test");
?

Here, the operator must be a member. Only few of the streaming operators for
std::cout are members.
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top