Question

If I have an object declared on the stack, and I return a reference to it, I believe I won't be able to access it anymore because it goes out of scope. Correct?

What if I just return the object itself (not a reference to it)? Will the copy constructor be called? (I have heard the term "move constructor", but from what I read, it seems to be a new feature. Can anybody elaborate a bit on it?)

In which cases will the destructor be called?

Was it helpful?

Solution

If I have an object declared on the stack, and I return a reference to it, I believe I won't be able to access it anymore because it goes out of scope. Correct?

Correct, and the destructor will be called when it goes out of scope.

What if I just return the object itself (not a reference to it)? Will the copy constructor be called?

A copy constructor and destructor MAY be called but typically the compiler will perform return value optimization and no copy, destructor or move will be made.

To find out about move constructors read up on move semantics and rvalue references.

OTHER TIPS

If I have an object declared on the stack, and I return a reference to it, I believe I won't be able to access it anymore because it goes out of scope. Correct?

Correct and many compiler would give you warning as well.

What if I just return the object itself (not a reference to it)? Will the copy constructor be called?

It would call copy constructor. std::array from standard does not have std::move constructor defined as internally it uses stack memory. Hence copy constructor would be called.

In which cases will the destructor be called?

In both cases destructor would be get called. Its just that after move constructor, we move all pointers/handled to other object hence the object state would be kind of empty(but consistent).

The compiler will try to optimize such functions in order to eliminate copy operations. The resulting code will behave as if the object was created at the point where the function would return it with appropriate lifetime. Reference here. This is dependent upon the compiler, and the structure of the code itself at both the construction and use points. The optimization may also not be possible, in which case you may have calls to copy or move constructor if available.

If the compiler optimizes the function, then the destructor will be called when the object at the use point goes out of nearest block scope.

If there is no optimization, then the object will be copied to another at the use point (one call to copy/move constructor), then the object in the function will be destroyed (if copied), and finally the destructor of the copied/moved object will be called when it goes out of nearest block scope at the location where the function returned it (use point).

However, if you return a temporary from a function then you will return a reference or pointer to destructed memory because your temporary will go out of scope upon function return and be destroyed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top