PDA

View Full Version : this is a c++ statement im confused with


bootyburglar
08-28-2003, 10:49 AM
Hello everyone, im new (as if you cant tell), im having a tuff time understanding what the following statement does;

friend ostream &operator<<(stuff......);

i understadn how to overload operators, and the friends of a class, but what im confused about is the<-- &--> reference operator, what is it doing in there, how would i read this statement to understand its purpose in this line of code (and any other line). I usually use it with pointers (duh), but now its sorta "standalone", please help :-)

Progr@mmEr
08-28-2003, 02:08 PM
The & in this example goes with ostream, and it means that this function will return an ostream reference. This enables cascading.
That means, that you can output many objects in one cout statement:

cout << object1 << object2 << object3;

After the object1 is output, with a call to function operator<<( cout, object1 );, this function will return a reference to cout, which means that you now have this statement:

cout << object2 << object3;

then cout << object2 will return a reference to cout, which means that you'll then have :

cout << object3;

etc.

if you did not return a reference to cout, you could only output ony 1 object in one cout statement.

it's simple :)