4 Replies - 162 Views - Last Post: 07 February 2012 - 07:41 AM Rate Topic: -----

Topic Sponsor:

#1 bnc  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 102
  • Joined: 18-March 10

returning reference

Posted 07 February 2012 - 05:44 AM

class Key
{
};

Key& make_key()
{
   return Key();
}

Key& key = make_key();
Key key1 = make_key();

Key make_key()
{
   return Key();
}

Key& key3 = make_key();
Key key4 = make_key();

Key& make_key()
{
    Key k;
    return k;
}

Key& key5 = make_key();
Key key6 = make_key();

Key make_key()
{
    key k;
    return k;
}

key& key7 = make_key();
Key key8 = make_key();



Can somebody please explain these scenarios.. not able to understand ... Thankyou

Is This A Good Question/Topic? 0
  • +

Replies To: returning reference

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 621
  • View blog
  • Posts: 1,038
  • Joined: 21-June 11

Re: returning reference

Posted 07 February 2012 - 05:59 AM

All of those except key4 and key8 invoke undefined behavior.

Key& make_key()
{
   return Key();
}



Here you're creating a temporary object by invoking the Key() constructor and then returning a reference to that object. Since the temporary object will be destroyed right away, the behavior of accessing that reference will be undefined.

Key make_key()
{
   return Key();
}



Here you're creating a temporary object by invoking the Key() constructor and then returning a copy of that object. This is fine.

Key& key3 = make_key();



Here you're using make_key to create a temporary object and then storing a reference to it in key3. Accessing key3 will invoke UB for the same reasons described above.

Key key4 = make_key();



Here you're storing a copy of the object created by make_key in key4. This is fine.

Key& make_key()
{
    Key k;
    return k;
}



Here you're creating a Key object as a local variable and returning a reference to it. Since local variables are destroyed when the function returns, this is as undefined as returning a reference to a temporary object.

Key make_key()
{
    key k;
    return k;
}



Here you're creating a Key object as a local variable and returning a copy of it. Again this is fine.

key& key7 = make_key();
Key key8 = make_key();



These behave the same as key3 and key4 respectively.

This post has been edited by sepp2k: 07 February 2012 - 05:59 AM

Was This Post Helpful? 1
  • +
  • -

#3 bnc  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 102
  • Joined: 18-March 10

Re: returning reference

Posted 07 February 2012 - 07:03 AM

@sepp2k

Thanks for your reply....


What I understand is when you are returning a local variable by reference it is wrong... So Key5 and Key6 are eliminated...

But for Key7 you are returning a key by value.
Key& key7 = make_key();


but that key will be valid in that statement. After we came out of that statement we are pointing a reference to a nonscoped Key.. is it right???
So Key7 eliminated..

edited**********************************
do not consider this line... Similar case with Key3.. Key3 eliminated
******************************************

Key8 is fine... Key4 is fine...


Canyou explain more on key and key1 (first set of keys)...

But what I know is(may be wrong.. correct me If I am wrong)

When you say
return Key();



the Key object will be created on the stack of the calle(one who calls this). So key and key1 (edited) and also key3 (edited) should be fine right???

Thankyou...

This post has been edited by bnc: 07 February 2012 - 07:29 AM

Was This Post Helpful? 0
  • +
  • -

#4 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 621
  • View blog
  • Posts: 1,038
  • Joined: 21-June 11

Re: returning reference

Posted 07 February 2012 - 07:34 AM

View Postbnc, on 07 February 2012 - 03:03 PM, said:

But what I know is(may be wrong.. correct me If I am wrong)

When you say
return Key();



the Key object will be created on the stack of the calle(one who calls this). So key and key1 should be fine right???


No. A temporary object does not live beyond the expression in which it appears. Once that expression is finished, the object is destroyed. It is not true that a temporary object created in one function will end up on the caller's stack (unless it is copied there of course¹).

¹ Such a copy may be elided for performance reasons causing the object to be in fact created directly on the caller's stack. But that's only relevant when returning by value, not by reference.
Was This Post Helpful? 1
  • +
  • -

#5 bnc  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 102
  • Joined: 18-March 10

Re: returning reference

Posted 07 February 2012 - 07:41 AM

Ok.. Thank you...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1