returning copy of the temporary string object

Discussion in 'C++' started by qazmlp, Mar 7, 2004.

  1. qazmlp

    qazmlp Guest

    Is this code reliable? Basically, I want to know whether returning
    copy of the temporary string object is valid or not.

    #include <iostream>

    std::string returnString()
    {
    return std::string( "TempString" ) ;
    }

    int main()
    {
    std::cout<< returnString() << std::endl;
    }
     
    qazmlp, Mar 7, 2004
    #1
    1. Advertising

  2. qazmlp

    Jeff Schwab Guest

    qazmlp wrote:
    > Is this code reliable? Basically, I want to know whether returning
    > copy of the temporary string object is valid or not.
    >
    > #include <iostream>
    >
    > std::string returnString()
    > {
    > return std::string( "TempString" ) ;
    > }
    >
    > int main()
    > {
    > std::cout<< returnString() << std::endl;
    > }


    Yep, that's fine.
     
    Jeff Schwab, Mar 7, 2004
    #2
    1. Advertising

  3. "qazmlp" <> wrote in message
    news:...
    > Is this code reliable? Basically, I want to know whether returning
    > copy of the temporary string object is valid or not.
    >
    > #include <iostream>
    >
    > std::string returnString()
    > {
    > return std::string( "TempString" ) ;
    > }
    >
    > int main()
    > {
    > std::cout<< returnString() << std::endl;
    > }


    Yes completely reliable.

    john
     
    John Harrison, Mar 7, 2004
    #3
  4. "qazmlp" <> wrote in message
    news:...
    > Is this code reliable? Basically, I want to know whether returning
    > copy of the temporary string object is valid or not.
    >
    > #include <iostream>
    >
    > std::string returnString()
    > {
    > return std::string( "TempString" ) ;
    > }
    >
    > int main()
    > {
    > std::cout<< returnString() << std::endl;
    > }


    It's reliable in Standard C++. I've seen Microsoft Visual C++ crash while trying
    to do that. A fair (and rhetorical question) would be, "Is Visual C++ reliable?"
    :)

    Claudio Puviani
     
    Claudio Puviani, Mar 7, 2004
    #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. Alexander Stippler
    Replies:
    2
    Views:
    633
    Alexander Stippler
    Jul 4, 2003
  2. Alex
    Replies:
    2
    Views:
    1,242
  3. jt
    Replies:
    4
    Views:
    582
    Fred L. Kleinschmidt
    May 16, 2005
  4. Replies:
    26
    Views:
    2,129
    Roland Pibinger
    Sep 1, 2006
  5. Replies:
    7
    Views:
    3,231
    James Kanze
    Feb 12, 2008
Loading...

Share This Page