Jump to content
  • Advertisement
Sign in to follow this  

correct way to return large objects from functions?

This topic is 4727 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

usually when returning objects from within the scope of a function I would have
A foo();
but with the advent of reference counting I have found myself doing
shared_ptr<A> foo();
what are your ideas on returning large objects from within the scope of a function? maybe making wrapper classes and using operator() to return the class members?
struct Foo
{
        const A &operator();
        A a;
};

bool Bar()
{
        Foo foo;
        A a = foo();
}
Although I think this is wasteful because Foo lasts until its out of scope, and it requires you to define it before using the method. thanks.

Share this post


Link to post
Share on other sites
Advertisement
Google for "Return Value Optimization" and "Move Constructor".

In your example, since the A object already exists before you call the function, why don't you just pass it by reference?

void foo(A&);


A a;
foo(a);

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!