reference to the temporary object

Discussion in 'C++' started by Raider, Jun 22, 2006.

  1. Raider

    Raider Guest

    is such code correct?

    #include <string>
    typedef std::string T;

    T *s;

    const T& foo(const T& default)
    {
    if (s) return *s; else return default;
    }

    int main()
    {
    const T& value = foo("bar");
    // use value
    }

    I think the "bar" must be destroyed immediatly when foo() returns.
    Raider, Jun 22, 2006
    #1
    1. Advertising

  2. Raider posted:

    > is such code correct?
    >
    > #include <string>
    > typedef std::string T;
    >
    > T *s;
    >
    > const T& foo(const T& default)
    > {
    > if (s) return *s; else return default;
    > }



    "default" is a keyword in C++.


    const T &foo(const T &def)
    {
    if(s) return *s;

    return def;
    }


    > int main()
    > {
    > const T& value = foo("bar");
    > // use value
    > }
    >
    > I think the "bar" must be destroyed immediatly when foo() returns.


    Step 1: A non-const, nameless, R-value temporary is created.
    Step 2: It is passed by const reference to a function.
    Step 3: The function then returns it by const reference.
    Step 4: The calling function binds a reference named "value" to the
    returned reference.

    If you bind a reference to a temporary, the temporary's lifetime is
    extended to that of the reference. However, there's only one Step in
    which this happens: Step 1. The argument to the function gets destroyed
    once the function returns. The function returns an invalid reference.
    You're left with a reference to an invalid object.
    Frederick Gotham, Jun 22, 2006
    #2
    1. Advertising

  3. Raider

    Raider Guest

    Frederick, thanks for support. I get it.

    > If you bind a reference to a temporary, the temporary's lifetime is
    > extended to that of the reference. However, there's only one Step in
    > which this happens: Step 1. The argument to the function gets destroyed
    > once the function returns. The function returns an invalid reference.
    > You're left with a reference to an invalid object.
    Raider, Jun 22, 2006
    #3
  4. Raider posted:

    > Frederick, thanks for support. I get it.
    >
    >> If you bind a reference to a temporary, the temporary's lifetime is
    >> extended to that of the reference. However, there's only one Step in
    >> which this happens: Step 1. The argument to the function gets

    destroyed
    >> once the function returns. The function returns an invalid reference.
    >> You're left with a reference to an invalid object.



    (Some gentle advice: If you top-post here, a lot of people will ignore
    you.)

    It's nice to try write code to prove things... I do it all the time! Take
    this for example:

    (Unchecked code)

    class ArbitraryClass
    {
    public:

    bool is_valid;

    ArbitraryClass() : is_valid(true) {}

    ~ArbitraryClass() { is_valid = false; }
    };


    #include <cstdlib>


    const ArbitaryClass &Func( const ArbitraryClass &arg )
    {
    static ArbitraryClass obj;

    if ( std::rand() == 52 ) return obj;

    return arg;
    }

    #include <iostream>

    int main()
    {
    const Arbitrary &ref = Func( ArbitraryClass() );

    std::cout << "Is the object referred to by ref valid? "
    << ( ref.is_valid ? "true" : "false" );

    }
    Frederick Gotham, Jun 22, 2006
    #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. John Ky
    Replies:
    9
    Views:
    436
    John Ky
    Feb 23, 2004
  2. Stefan Strasser
    Replies:
    2
    Views:
    8,164
    Victor Bazarov
    Mar 28, 2005
  3. Dave Rahardja
    Replies:
    15
    Views:
    1,171
    Alf P. Steinbach
    Nov 3, 2005
  4. Martin Rennix
    Replies:
    3
    Views:
    631
    Martin Rennix
    May 7, 2007
  5. Replies:
    7
    Views:
    3,211
    James Kanze
    Feb 12, 2008
Loading...

Share This Page