Fundamental C++ question
...but one that has only just occurred to me.
When you return an object by value, which runs first: the constructor of the object being returned, or the destructors of local objects in the function that is returning?
The reason it matters is: if I declare a boost::shared_ptr as a local variable in a function, and then immediately return it by value, will the reference count inside the shared_ptr go
1,2,1 ...leaving me with an allocated object pointed to by the return value.
or
1,0 ... leaving me with a smart pointer to bad data?
I'm assuming the former (purely because I'd think that shared_ptr would be designed to be at least as useful as a raw pointer!) but does anyone have a reference? Or some head-slapping examples of why it must obviously work one way or the other?
When you return an object by value, which runs first: the constructor of the object being returned, or the destructors of local objects in the function that is returning?
The reason it matters is: if I declare a boost::shared_ptr as a local variable in a function, and then immediately return it by value, will the reference count inside the shared_ptr go
1,2,1 ...leaving me with an allocated object pointed to by the return value.
or
1,0 ... leaving me with a smart pointer to bad data?
I'm assuming the former (purely because I'd think that shared_ptr would be designed to be at least as useful as a raw pointer!) but does anyone have a reference? Or some head-slapping examples of why it must obviously work one way or the other?
