+ works for string literal also

Discussion in 'C++' started by qazmlp, Jun 25, 2003.

  1. qazmlp

    qazmlp Guest

    int main()
    {
    std::string strVal = "2" ;
    strVal = "000" + strVal ;
    }

    I am wondering how the above one compiles well. I expected that, it should be

    int main()
    {
    std::string strVal = "2" ;
    strVal = std::string("000") + strVal ;
    }
     
    qazmlp, Jun 25, 2003
    #1
    1. Advertising

  2. qazmlp

    Ron Natalie Guest

    "qazmlp" <> wrote in message news:...
    > int main()
    > {
    > std::string strVal = "2" ;
    > strVal = "000" + strVal ;
    > }
    >
    > I am wondering how the above one compiles well. I expected that, it should be
    >

    An overload for operator+ is provided
    string operator+(const char*, const string& )

    (Not literally, but the above is good enough to understand the issue).

    This means you can mix most operations you can do with strings with char*'s pointing
    at null terminated strings as long as one operand is a string type (obviously no overload
    is possible if both args are pointers).

    "000" + str;
    str + "000"
    str + str;

    are all valid

    "000" + "000"

    is not.
     
    Ron Natalie, Jun 25, 2003
    #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.
Similar Threads
  1. Prakash Prabhu

    String literal and String Object

    Prakash Prabhu, Aug 27, 2003, in forum: Java
    Replies:
    3
    Views:
    644
    John C. Bollinger
    Aug 27, 2003
  2. Angus
    Replies:
    3
    Views:
    337
  3. Anonieko Ramos

    What's wrong with rpc-literal? Why use doc-literal?

    Anonieko Ramos, Sep 27, 2004, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    381
    Anonieko Ramos
    Sep 27, 2004
  4. Trans
    Replies:
    2
    Views:
    169
    Trans
    Aug 30, 2005
  5. lendle
    Replies:
    1
    Views:
    116
Loading...

Share This Page