Jump to content
  • Advertisement
Sign in to follow this  
robot ears

anonymous objects

This topic is 3003 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Say I have a data class, nothing fancy (i.e. no dynamic memory nor inheritance) and I want to return an object of that type in a non-related function (say another member function).

class Foo
{
public:
Foo::Foo(int); // no default ctor
};

class Goo
{
Foo getFoo();
};

Foo Goo::getFoo()
{
return Foo(5);
}

Now my question is: does the anonymous Foo object I created in getFoo() get returned? Does anybody out there really know the rules about anonymous objects?

Is the compiler-provided (shallow) copy constructor invoked, passing the object back by value? (I expect p-b-v is the only way to go...)

I realize anonymous objects have "expression-scope"; does that mean it exists until the constructor finishes, or when getFoo's "return" finishes?

Thanks ppl

Share this post


Link to post
Share on other sites
Advertisement
I believe that due to RVO and NRVO (Google them with the keyword C++) means that the compiler is allowed to omit the copies if it wishes. Good modern compilers will.

Its a strange corner case to be aware of, because this is an optimisation that can be performed even if your copy constructor is non-trivial or has other special side effects.

Share this post


Link to post
Share on other sites
Note that if you don't need to modify Foo in your code, you can reliably prevent any intermediate copies by returning a const reference.

A normal reference would be undefined, since the temporary object it refers to will be destroyed at the end of the assignment expression. However, not so in the case of a const reference, this is explicitely allowed. The temporary object will live for the duration of the const reference's lifetime.

Edit: See GotW #88 for a similar example.

Share this post


Link to post
Share on other sites
Quote:
Original post by robot ears
Now my question is: does the anonymous Foo object I created in getFoo() get returned? Does anybody out there really know the rules about anonymous objects?

Yes, and yes (the behaviour is well defined and has been well defined for about, oh, 20 years -- it is even codified in an international standard).
Quote:
Is the compiler-provided (shallow) copy constructor invoked, passing the object back by value? (I expect p-b-v is the only way to go...)

Yes. The compiler is allowed to elide the copy operation but a (possibly default) copy constructor must exist and be visible (ie. not private).
Quote:
I realize anonymous objects have "expression-scope"; does that mean it exists until the constructor finishes, or when getFoo's "return" finishes?

You are returning by value. You construct an object, then return it. There are in fact no temporaries involved, despite the fact that you have not named a variable of automatic storage duration.

Share this post


Link to post
Share on other sites
thanks ppl I appreciate it. I was expecting the behaviour I was describing but not seeing; my problem was a namespace clash. But now I'm using anonymous objects like a pro!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!