Jump to content
  • Advertisement
Sign in to follow this  
Genjix

correct way to return large objects from functions?

This topic is 4879 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
You ccan also pass by non-const reference:

[source

void foo(A& a)
{
a = ...
}

void bar()
{
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.

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!