question on cout and operator<<

Discussion in 'C++' started by subramanian100in@yahoo.com, India, Sep 19, 2007.

  1. , India

    , India Guest

    Consider the code:

    #include <iostream>

    using namespace std;

    int main( )
    {
    cout << "test string ";
    cout.operator<<(10).operator<<(endl);

    return 0;
    }

    This prints
    test string 10
    followed by a newline as expected.

    However, in main( ), if I have

    cout.operator<<("test string
    ").operator<<(endl).operator<<(10).operator<<(endl);

    it prints
    0x804897c
    10
    followed by a newline. It does not print test string but instead
    prints its address.

    I do not understand.
    Does it mean that operator<<(const char *) is not a member function of
    ostream ? If so why ?

    Kindly explain.

    Thanks
    V.Subramanian
    , India, Sep 19, 2007
    #1
    1. Advertising

  2. On 2007-09-19 08:37, , India wrote:
    > Consider the code:
    >
    > #include <iostream>
    >
    > using namespace std;
    >
    > int main( )
    > {
    > cout << "test string ";
    > cout.operator<<(10).operator<<(endl);
    >
    > return 0;
    > }
    >
    > This prints
    > test string 10
    > followed by a newline as expected.
    >
    > However, in main( ), if I have
    >
    > cout.operator<<("test string
    > ").operator<<(endl).operator<<(10).operator<<(endl);
    >
    > it prints
    > 0x804897c
    > 10
    > followed by a newline. It does not print test string but instead
    > prints its address.
    >
    > I do not understand.
    > Does it mean that operator<<(const char *) is not a member function of
    > ostream ? If so why ?


    You are correct, but as to why it is not a member I do not know. Last
    time I saw this discussed none seemed to be able to come up with a good
    reason, you could try asking in comp.std.c++ though.

    --
    Erik Wikström
    =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=, Sep 19, 2007
    #2
    1. Advertising

  3. , India

    , India Guest

    On Sep 19, 2:40 pm, Erik Wikström <> wrote:
    > On 2007-09-19 08:37, , India wrote:
    >
    >
    >
    > > Consider the code:

    >
    > > #include <iostream>

    >
    > > using namespace std;

    >
    > > int main( )
    > > {
    > > cout << "test string ";
    > > cout.operator<<(10).operator<<(endl);

    >
    > > return 0;
    > > }

    >
    > > This prints
    > > test string 10
    > > followed by a newline as expected.

    >
    > > However, in main( ), if I have

    >
    > > cout.operator<<("test string
    > > ").operator<<(endl).operator<<(10).operator<<(endl);

    >
    > > it prints
    > > 0x804897c
    > > 10
    > > followed by a newline. It does not print test string but instead
    > > prints its address.

    >
    > > I do not understand.
    > > Does it mean that operator<<(const char *) is not a member function of
    > > ostream ? If so why ?

    >
    > You are correct, but as to why it is not a member I do not know. Last
    > time I saw this discussed none seemed to be able to come up with a good
    > reason, you could try asking in comp.std.c++ though.
    >
    > --
    > Erik Wikström


    I just now found a member function
    operator<<(const void * val)

    Could the reason be the following :
    If we wanted to print some address, we have to call
    cout.operator<<(ptr) EXPLICITLY for any pointer ptr. So, by calling
    cout.operator<<("test string"), the compiler assumes that we want to
    print the address and so it prints 0x804897c
    When we give cout << "test string", the overloaded non-member function
    is called to print the string literal itself.

    Is this reasoning correct ? Excuse me if I am wrong.

    Kindly explain.

    Thanks
    V.Subramanian
    , India, Sep 20, 2007
    #3
  4. On 2007-09-20 05:13, , India wrote:
    > On Sep 19, 2:40 pm, Erik Wikström <> wrote:
    >> On 2007-09-19 08:37, , India wrote:
    >>
    >>
    >>
    >> > Consider the code:

    >>
    >> > #include <iostream>

    >>
    >> > using namespace std;

    >>
    >> > int main( )
    >> > {
    >> > cout << "test string ";
    >> > cout.operator<<(10).operator<<(endl);

    >>
    >> > return 0;
    >> > }

    >>
    >> > This prints
    >> > test string 10
    >> > followed by a newline as expected.

    >>
    >> > However, in main( ), if I have

    >>
    >> > cout.operator<<("test string
    >> > ").operator<<(endl).operator<<(10).operator<<(endl);

    >>
    >> > it prints
    >> > 0x804897c
    >> > 10
    >> > followed by a newline. It does not print test string but instead
    >> > prints its address.

    >>
    >> > I do not understand.
    >> > Does it mean that operator<<(const char *) is not a member function of
    >> > ostream ? If so why ?

    >>
    >> You are correct, but as to why it is not a member I do not know. Last
    >> time I saw this discussed none seemed to be able to come up with a good
    >> reason, you could try asking in comp.std.c++ though.
    >>
    >> --
    >> Erik Wikström

    >
    > I just now found a member function
    > operator<<(const void * val)
    >
    > Could the reason be the following :
    > If we wanted to print some address, we have to call
    > cout.operator<<(ptr) EXPLICITLY for any pointer ptr. So, by calling
    > cout.operator<<("test string"), the compiler assumes that we want to
    > print the address and so it prints 0x804897c
    > When we give cout << "test string", the overloaded non-member function
    > is called to print the string literal itself.
    >
    > Is this reasoning correct ? Excuse me if I am wrong.


    Not quite, the only overload of the << operator which takes a pointer is
    the one for the char* (or perhaps is was const char*), if you want to
    print any other pointer you can use the operator as normal:

    #include <iostream>

    int main()
    {
    int* i = new int(1);
    std::cout << i;
    }

    I would assume that the reason for overloading for char* is to allow the
    common usage of printing a string literal:

    std::cout << "Hello World";

    which would not work otherwise.

    --
    Erik Wikström
    =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=, Sep 20, 2007
    #4
    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,647
  2. Pmb

    std::cout vs cout

    Pmb, Jun 2, 2004, in forum: C++
    Replies:
    2
    Views:
    4,404
    Leor Zolman
    Jun 2, 2004
  3. Replies:
    4
    Views:
    316
    Howard
    Sep 15, 2005
  4. Stefan Ram

    Re: cout vs std::cout

    Stefan Ram, Sep 28, 2008, in forum: C++
    Replies:
    7
    Views:
    475
  5. Hendrik Schober

    Re: cout vs std::cout

    Hendrik Schober, Sep 28, 2008, in forum: C++
    Replies:
    7
    Views:
    437
    Jerry Coffin
    Oct 7, 2008
Loading...

Share This Page