Topic: What does "this" << "that return to cout?


Author: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
Date: 1995/04/04
Raw View
xan@iinet.com.au (Phil Kan) writes:

>I'm new to the language (after years of C), so please bear with me.
>
>I understand that the construct:
>
>cout << "string " << function_returning_string() << " string";
>
>it is evaluated as
>
>cout << "string " << (function_returning_string() << " string");
>
>Is this correct?

No.  It gets parsed as

 ((cout << "string ") << function_returning_string()) << " string";

Each of the bracketed expressions returns an `ostream &' which refers
to `cout'.

--
Fergus Henderson - fjh@munta.cs.mu.oz.au





Author: barry.pryde@ltn.com (BARRY PRYDE)
Date: 1995/03/31
Raw View

   PK   cout << "string " << function_returning_string() << " string";


It can be any type that the compiler supports. That is the power of
cout. It allows for any input to be sent. You can say cout << 89567;
without a problem.

This allows you to do things like:

cout << "The date to day is " << Mday << " of " << Mmonth << "," << Myear;

Hope that helps.




==============================================================================

                        ############
                      ##            ##
                    ##                ##
                  ##                    ##
                 ##    #####    #####    ##
                ##    #  x  #  #  x  #    ##
-------####---------------- #  # --------------####----------
|                           #  #                            |
|                            ##                             |
| Barry Pryde - Systems Development: London Telecom Network |
|               SYSOP: London Telecom BBS                   |
|              E-Mail: barry.pryde@ltn.com                  |
-------------------------------------------------------------

... I distinctly remember forgetting that!
---
     TLX v4.10



 * 1st 2.00d #1981s * London Telecom BBS (905-570-8956 Hamilton, Ont, Canada)





Author: xan@iinet.com.au (Phil Kan)
Date: 1995/03/31
Raw View
Yo all...

I'm new to the language (after years of C), so please bear with me.

I understand that the construct:

cout << "string " << function_returning_string() << " string";

it is evaluated as

cout << "string " << (function_returning_string() << " string");

Is this correct? If so, what does the first evaluated (the bit
inside the brackets) expression return? Is it a char *, const char *,
or something else?

thanx for all responses.
Email or post is fine,

TC&GB,
Phil Kan.
xan@iinet.com.au





Author: russell@news.mdli.com (Russell Blackadar)
Date: 1995/03/31
Raw View
Phil Kan (xan@iinet.com.au) wrote:
: Yo all...

: I'm new to the language (after years of C), so please bear with me.

: I understand that the construct:

: cout << "string " << function_returning_string() << " string";

: it is evaluated as

: cout << "string " << (function_returning_string() << " string");

: Is this correct? If so, what does the first evaluated (the bit
: inside the brackets) expression return? Is it a char *, const char *,
: or something else?

   It's not correct; exactly as in C, >> associates left-to-right.
   Unless you have a very strange string class, the expression in
   parentheses will not even compile.  (You'd need a string::operator<<
   that takes a const char* argument; what would that mean?  And if by
   string you mean char*, it would be impossible.)

   Without the parentheses, the type is ostream&, as defined in
   iostream.h.

: thanx for all responses.

   You're welcome.

: Email or post is fine,

: TC&GB,
: Phil Kan.
: xan@iinet.com.au





Author: sailer@a4430edc.esr.hp.com (Lee Sailer)
Date: 1995/03/31
Raw View
In article <3lfqqg$brq@classic.iinet.com.au>, Phil Kan (xan@iinet.com.au) wrote:
> Yo all...

> I'm new to the language (after years of C), so please bear with me.

> I understand that the construct:

> cout << "string " << function_returning_string() << " string";

> it is evaluated as

> cout << "string " << (function_returning_string() << " string");

> Is this correct? If so, what does the first evaluated (the bit

No.  << evaluates from left to right.

(cout << "string ") << function_returning_string() << " string";

which is why the first << returns cout.

> inside the brackets) expression return? Is it a char *, const char *,
> or something else?

> thanx for all responses.
> Email or post is fine,

> TC&GB,
> Phil Kan.
> xan@iinet.com.au

--
lee






Author: admin@rzaix13.uni-hamburg.de (Bernd Eggink)
Date: 1995/03/31
Raw View
Phil Kan (xan@iinet.com.au) wrote:
>  Yo all...

>  I'm new to the language (after years of C), so please bear with me.

>  I understand that the construct:

>  cout << "string " << function_returning_string() << " string";

>  it is evaluated as

>  cout << "string " << (function_returning_string() << " string");

>  Is this correct? If so, what does the first evaluated (the bit
>  inside the brackets) expression return? Is it a char *, const char *,
>  or something else?

No. operator<< associates left to right. The expression is evaluated as

  ((cout << "string") << function_returning_string()) << "string";

The return type of ostream::operator<<(whatever) is ostream& .

--
+----------------------------------+
|          Bernd Eggink            |
|    Rechenzentrum Uni Hamburg     |
| admin@rzaix13.rrz.uni-hamburg.de |
+----------------------------------+