<< overloading

P

Prasanth

friend ostream& operator<< (ostream &out, Point &cPoint)

I have encountered this statement in the operator over loading of
Input and output streams.

cout << cP1 << endl;

What does the first Parameter Contain i.e) ostream &out ???

Thanks,
Prasanth
 
I

Ian Collins

friend ostream& operator<< (ostream&out, Point&cPoint)
The canonical form would be

friend ostream& operator<<( ostream& out, const Point& cPoint)
^^^^^
I have encountered this statement in the operator over loading of
Input and output streams.

cout<< cP1<< endl;

What does the first Parameter Contain i.e) ostream&out ???

Whatever is on the left hand side of the operator, in this case cout.
You can think of the call as "operator<<( cout, cP1 )".
 
P

Prasanth

On 05/ 1/11 06:58 PM, Prasanth wrote:> friend ostream&  operator<<  (ostream&out, Point&cPoint)

The canonical form would be

friend ostream& operator<<( ostream& out, const Point& cPoint)
                                           ^^^^^




Whatever is on the left hand side of the operator, in this case cout.
You can think of the call as "operator<<( cout, cP1 )".

then can the statement cout << cP1 << cP2; be considered as a
recursive function
operator(operator(cout,cP1),cP2); ???
 
K

Kai-Uwe Bux

Prasanth said:
then can the statement cout << cP1 << cP2; be considered as a
recursive function
operator(operator(cout,cP1),cP2); ???

Yup, it's a nested call. That also explains why you want operator<< to
return an ostream&.


Best,

Kai-Uwe Bux
 
A

AnonMail2005

Yup, it's a nested call. That also explains why you want operator<< to
return an ostream&.

Best,

Kai-Uwe Bux

I don't think it's a "nested" call - it's just two calls to the
operator << function.
The first call takes cout as the 1st arg and cP1 as the 2nd arg.
The second call takes the return value of the first function (in this
case cout) as it's 1st arg and cP2 as it's 2nd arg.
No nested or recursive function calls.

HTH
 
K

Kai-Uwe Bux

I don't think it's a "nested" call - it's just two calls to the
operator << function.
The first call takes cout as the 1st arg and cP1 as the 2nd arg.
The second call takes the return value of the first function (in this
case cout) as it's 1st arg and cP2 as it's 2nd arg.
No nested or recursive function calls.

I guess, we just differ in the use of "nested". In f(g(a,b),c), I think of
the call of g as being nested withing the call of f. I agree that there is
no recursion. Also, I don't stipulate that f is calling g. Nestedness here
is purely on the level of the expression f(g(a,b),c).

Nonetheless, the OP has chosen the word recursive to allude to a certain
syntactical phenomenon. I was not happy with the word and replaced it by
"nested". Maybe that choice was not perfect. However, there is a certain
syntactical aspect of the expression f( f(...), ...) that the OP
legitimately wanted to stress. So what is the right word here?


Best,

Kai-Uwe Bux
 
A

a.s.

I guess, we just differ in the use of "nested". In f(g(a,b),c), I think of
the call of g as being nested withing the call of f. I agree that there is
no recursion. Also, I don't stipulate that f is calling g. Nestedness here
is purely on the level of the expression f(g(a,b),c).

Nonetheless, the OP has chosen the word recursive to allude to a certain
syntactical phenomenon. I was not happy with the word and replaced it by
"nested". Maybe that choice was not perfect. However, there is a certain
syntactical aspect of the expression f( f(...), ...)  that the OP
legitimately wanted to stress. So what is the right word here?

Best,

Kai-Uwe Bux

Hello,

I agree to that the OP should have been corrected regarding the
terminology: referring to a function as being 'nested' should be with
respect to how the function is structured structure, not how the
function it is being called.

Just so that we do not get blamed for bad exam results :)

Regards,
Al-Asham Said
 

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,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top