How should I return a large class object from a function?

Hello.

If I have a class, for example a LinkedList, and I want to create a function called Reverse() which returns a new just created LinkedList object which has the same values of the original list but reversed...

How should I return the new created object?

By reference (&) or by address (*)?

Note that in a certain point there will be an IF which controls if the Head of the list is nullptr or not.

If it is, with return-by-address I can simply say return nullptr... with return-by-reference I cannot.

Also, I don't want to return by value. I'm just pretending it's a huge class: would you ever return-by-value a huge object?
Last edited on
Neither. Just return by value. In C++03 the compiler will use copy elision to optimize it, and in C++11 you will just be using move semantics. As long as you have a move constructor or a move assignment operator, it will be a cheap operation.
Oh thanks for the tip!

I've just read page 517 from Bjarne Stroustrup's "The C++ Programming Language" and there's what you just told me.

Thanks!
Topic archived. No new replies allowed.