<< overloading

Discussion in 'C++' started by Prasanth, May 1, 2011.

  1. Prasanth

    Prasanth Guest

    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
     
    Prasanth, May 1, 2011
    #1
    1. Advertising

  2. Prasanth

    Ian Collins Guest

    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)
    ^^^^^

    > 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 )".

    --
    Ian Collins
     
    Ian Collins, May 1, 2011
    #2
    1. Advertising

  3. Prasanth

    Prasanth Guest

    On May 1, 12:51 pm, Ian Collins <> wrote:
    > 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)
    >                                            ^^^^^
    >
    > > 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 )".
    >
    > --
    > Ian Collins


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

    --
    Prasanth
     
    Prasanth, May 1, 2011
    #3
  4. Prasanth

    Kai-Uwe Bux Guest

    Prasanth wrote:

    > On May 1, 12:51 pm, Ian Collins <> wrote:
    >> 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)
    >> ^^^^^
    >>
    >> > 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 )".
    >>
    >> --
    >> Ian Collins

    >
    > 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
     
    Kai-Uwe Bux, May 1, 2011
    #4
  5. Prasanth

    Guest

    On May 1, 10:39 am, Kai-Uwe Bux <> wrote:
    > Prasanth wrote:
    > > On May 1, 12:51 pm, Ian Collins <> wrote:
    > >> 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)
    > >> ^^^^^

    >
    > >> > 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 )".

    >
    > >> --
    > >> Ian Collins

    >
    > > 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


    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
     
    , May 1, 2011
    #5
  6. Prasanth

    Kai-Uwe Bux Guest

    wrote:

    > On May 1, 10:39 am, Kai-Uwe Bux <> wrote:
    >> Prasanth wrote:
    >> > On May 1, 12:51 pm, Ian Collins <> wrote:
    >> >> 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)
    >> >> ^^^^^

    >>
    >> >> > 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 )".

    >>
    >> >> --
    >> >> Ian Collins

    >>
    >> > 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

    >
    > 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
     
    Kai-Uwe Bux, May 1, 2011
    #6
  7. Prasanth

    a.s. Guest

    On May 1, 9:11 pm, Kai-Uwe Bux <> wrote:
    > wrote:
    > > On May 1, 10:39 am, Kai-Uwe Bux <> wrote:
    > >> Prasanth wrote:
    > >> > On May 1, 12:51 pm, Ian Collins <> wrote:
    > >> >> 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)
    > >> >> ^^^^^

    >
    > >> >> > 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 )".

    >
    > >> >> --
    > >> >> Ian Collins

    >
    > >> > 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

    >
    > > 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


    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
     
    a.s., May 3, 2011
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Iyer, Prasad C

    Overloading __init__ & Function overloading

    Iyer, Prasad C, Sep 30, 2005, in forum: Python
    Replies:
    3
    Views:
    6,451
    Fredrik Lundh
    Sep 30, 2005
  2. Fredrik Lundh
    Replies:
    0
    Views:
    477
    Fredrik Lundh
    Sep 30, 2005
  3. Steve Holden
    Replies:
    0
    Views:
    453
    Steve Holden
    Sep 30, 2005
  4. Iyer, Prasad C
    Replies:
    4
    Views:
    603
    John J. Lee
    Sep 30, 2005
  5. Fredrik Lundh
    Replies:
    0
    Views:
    421
    Fredrik Lundh
    Sep 30, 2005
Loading...

Share This Page