cout2,

Discussion in 'C++' started by vsgdp, Nov 7, 2006.

  1. vsgdp

    vsgdp Guest

    Why does the first statement output nonsense, but not the second. That
    is, why aren't the statements equivelement?

    #include<iostream>
    #include <cmath>
    using namespace std;

    int main()
    {
    cout.operator <<("sin(2)=").operator <<(sin(2)).operator <<(endl);
    cout << "sin(2)=" << sin(2) << endl;
    }
     
    vsgdp, Nov 7, 2006
    #1
    1. Advertising

  2. vsgdp wrote:
    > Why does the first statement output nonsense, but not the second. That
    > is, why aren't the statements equivelement?
    >
    > #include<iostream>
    > #include <cmath>
    > using namespace std;
    >
    > int main()
    > {
    > cout.operator <<("sin(2)=").operator <<(sin(2)).operator <<(endl);


    cout.operator << "sin(2)="; invokes the member function
    ostream::eek:perator<< (const void* param)
    and not what you expected:
    inline ostream operator<< (ostream& _O, const char *param)
    The reason for this is that you explicitely ask for the member function
    operator<<, but operator<< for char* is defined as static function (as
    most other custom defined stream operators are). What you are seeing is
    the address of the string.

    > cout << "sin(2)=" << sin(2) << endl;
    > }


    You should always stick to this invokation scheme.

    Regards,
    Stuart
     
    Stuart Redmann, Nov 7, 2006
    #2
    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.

Share This Page