temporary object

Discussion in 'C++' started by Tony Johansson, May 20, 2005.

  1. Hello!!

    Assume I have a wrapper class with this definition.
    class Integer
    {
    public:
    Integer(int =0);
    int get() const;
    Integer& set(int);
    Integer& add(const Integer&);
    private:
    int value_;
    };

    I have also some stand-alone functions se below
    Now to my question the function createZero is returning an Integer object
    lets call this object temp.
    This temp object is received as a reference in function yuk. No copy
    constructor is called therefore.
    So does this mean that when yuk is eventually finishing the temp object goes
    out of scope.
    So the assignment from function foo to object g will be invalid because of
    temp is out of scope.

    //Tony

    Integer createZero()
    { return Integer(0); }

    const Integer& yuk(const Integer& i)
    { return i; }

    void foo()
    {
    const Integer& g = yuk(creteZero());
    cout << g.value();
    }
     
    Tony Johansson, May 20, 2005
    #1
    1. Advertising

  2. Tony Johansson wrote:
    > Assume I have a wrapper class with this definition.
    > class Integer
    > {
    > public:
    > Integer(int =0);
    > int get() const;
    > Integer& set(int);
    > Integer& add(const Integer&);
    > private:
    > int value_;
    > };
    >
    > I have also some stand-alone functions se below
    > Now to my question the function createZero is returning an Integer object
    > lets call this object temp.
    > This temp object is received as a reference in function yuk. No copy
    > constructor is called therefore.
    > So does this mean that when yuk is eventually finishing the temp object goes
    > out of scope.


    The temporary is destroyed when the reference [originally] bound to it
    (the argument of 'yuk') ceases to exist, and that's at the end of the
    "full expression", the left side of the initialisation of 'g'.

    > So the assignment from function foo to object g


    It's not assignment, it's initialisation. And the function is 'yuk', not
    'foo'.

    > will be invalid because of
    > temp is out of scope.


    Yes. 'g' is initialised to the reference value that becomes invalid right
    after that, and so does 'g'. The program has undefined behaviour.

    >
    > //Tony
    >
    > Integer createZero()
    > { return Integer(0); }
    >
    > const Integer& yuk(const Integer& i)
    > { return i; }
    >
    > void foo()
    > {
    > const Integer& g = yuk(creteZero());
    > cout << g.value();
    > }


    V
     
    Victor Bazarov, May 20, 2005
    #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. John Ky
    Replies:
    9
    Views:
    444
    John Ky
    Feb 23, 2004
  2. qazmlp
    Replies:
    3
    Views:
    399
    Claudio Puviani
    Mar 7, 2004
  3. Stefan Strasser
    Replies:
    2
    Views:
    8,194
    Victor Bazarov
    Mar 28, 2005
  4. Jess
    Replies:
    10
    Views:
    510
    James Kanze
    May 18, 2007
  5. Replies:
    7
    Views:
    3,262
    James Kanze
    Feb 12, 2008
Loading...

Share This Page