Why does << operator work only when there is at least one cout?

Discussion in 'C++' started by Mc Lauren Series, Jun 9, 2009.

  1. 1. cout << "hello, " << "world" << endl;
    2. "hello, " << "world" << endl;
    3. printf("hello, " << "world" << endl);

    Why is it that statement 1 is valid but statements 2 and 3 are not?
     
    Mc Lauren Series, Jun 9, 2009
    #1
    1. Advertising

  2. Mc Lauren Series

    Guest

    On Jun 9, 8:07 am, Mc Lauren Series <> wrote:
    > 1. cout << "hello, " << "world" << endl;
    > 2. "hello, " << "world" << endl;
    > 3. printf("hello, " << "world" << endl);
    >
    > Why is it that statement 1 is valid but statements 2 and 3 are not?


    Hi

    Because the standard say so:
    ostream& operator<<(ostream&, const char*);
    As you know, calling overloaded operator is exactly like calling non-
    local or
    member function. The above declaration means: get an ostream object
    and a char string,
    put the char string to ostream object and return it. So when we write:
    cout << "Hello, "
    the following statement is executed:
    operator<<(cout , "Hello, ");
    and of course it return the stream object so we can put another string
    (in this case "world") to output.
    In the following statement:
    "hello, " << "world" << endl;
    how to say my intend is output operation? We can reason about
    statement 3. in the same way.

    I hope it helps.

    -- Saeed Amrollahi
     
    , Jun 9, 2009
    #2
    1. Advertising

  3. Mc Lauren Series

    Guest

    On Jun 8, 10:07 pm, Mc Lauren Series <> wrote:
    > 1. cout << "hello, " << "world" << endl;
    > 2. "hello, " << "world" << endl;
    > 3. printf("hello, " << "world" << endl);
    >
    > Why is it that statement 1 is valid but statements 2 and 3 are not?


    You need a little more firm understanding of the language to really
    understand why. Basically, you need to pick up a decent book on teach
    yourself C++, or an excellent website, etc.

    The short answer is because the language rules say so.

    A slightly longer answer is that cout is an object which overloads the
    operator<< to do formatted output to the standard out of the process,
    which most implementations put to your screen.
     
    , Jun 9, 2009
    #3
  4. Mc Lauren Series

    Tim Slattery Guest

    Mc Lauren Series <> wrote:

    >1. cout << "hello, " << "world" << endl;
    >2. "hello, " << "world" << endl;
    >3. printf("hello, " << "world" << endl);
    >
    >Why is it that statement 1 is valid but statements 2 and 3 are not?


    Because it's a function of the cout object. It causes the operand on
    the right side to be output, and it returns the cout object, so that
    it can be chained, as in line 1.

    --
    Tim Slattery

    http://members.cox.net/slatteryt
     
    Tim Slattery, Jun 9, 2009
    #4
  5. Mc Lauren Series

    James Kanze Guest

    On Jun 9, 7:07 am, Mc Lauren Series <> wrote:
    > 1. cout << "hello, " << "world" << endl;
    > 2. "hello, " << "world" << endl;
    > 3. printf("hello, " << "world" << endl);


    > Why is it that statement 1 is valid but statements 2 and 3 are
    > not?


    Because there is an operator<< defined with an std::eek:stream (the
    type of std::cout) as left argument, but not for char const[]
    (the type of a string literal).

    What should 2 or 3 mean?

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Jun 10, 2009
    #5
  6. Mc Lauren Series

    James Kanze Guest

    On Jun 9, 3:29 pm, Tim Slattery <> wrote:
    > Mc Lauren Series <> wrote:


    > >1. cout << "hello, " << "world" << endl;
    > >2. "hello, " << "world" << endl;
    > >3. printf("hello, " << "world" << endl);


    > >Why is it that statement 1 is valid but statements 2 and 3 are not?


    > Because it's a function of the cout object.


    Actually, it's not. It's a free function. (At least in
    standard C++. In the classical iostream, before the standard,
    it was a member.)

    The important point is that 1) there is a << operator defined
    which takes an std::istream (the type of std::cout) as its left
    argument, and 2) as you point out, the result of this operator
    is also an std::istream, so you can chain the operations.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Jun 10, 2009
    #6
    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. abi

    man cout or info cout

    abi, Jun 27, 2003, in forum: C++
    Replies:
    2
    Views:
    1,672
  2. Pmb

    std::cout vs cout

    Pmb, Jun 2, 2004, in forum: C++
    Replies:
    2
    Views:
    4,447
    Leor Zolman
    Jun 2, 2004
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,226
    Smokey Grindel
    Dec 2, 2006
  4. Replies:
    4
    Views:
    342
    Howard
    Sep 15, 2005
  5. AAaron123
    Replies:
    0
    Views:
    648
    AAaron123
    Oct 3, 2008
Loading...

Share This Page