Sending std::cout to std::cout !!

S

saurabh29789

I have 2 statements :

S1 : std::cout<<std::cout<<"Hi";

and

S2 : std::cout<<(std::cout<<"Hi");


S1 prints an address followed by Hi on the screen while S2 prints Hi
followed by the same address.

I know about cascading but am not able to understand what is happening
here!!
 
H

Harald Finster

Hi
I have 2 statements :

S1 : std::cout<<std::cout<<"Hi";

this is evaluated from left to right:

1) std::cout << std::cout :
prints the address to cout
returns std::cout

2) the result, i.e. std::cout is "operated" with "Hi",
i.e. std::cout << "Hi"
this prints "Hi" to cout

and

S2 : std::cout<<(std::cout<<"Hi");

the term in brackets is evaluated at first, i.e.

1) std::cout << "Hi"
prints "Hi" to cout
returns std::cout

2) the result (again std::cout) is "operated" with
the leftmost std::cout, i.e. std::cout<<std::cout
this prints the address od std::cout (the righthand operand of <<)

Again: remember, that x<<y can be written as operator<<(x,y)

The first statement can be written as:

operator<<( operator<<( std::cout, std::cout ) , "Hi );

the second one as

operator<<( std::cout, operator<<( std::cout , "Hi" ) )


Greetings Harald
 

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,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top