If your compiler implements Return Value Optimization (RVO) then it can set up the call to f() such that s is constructed where the caller would store the return value, and therefore it can elide the calls to the CString copy constructor and destructor. This optimization is one of the few exceptions permitted by the C++ standard to the as-if optimization rule.
If you are compiling with all optimizations disabled, you would likely see one or more calls to the CString copy constructor and destructor in processing the call to f().
0
Luis Colorado
On
The compiler will call the destructor of any object that ceases to exist when you finish function execution. As s is declared local to the function f(), al local objects (as s is) will cease to exist and as such, the compiler will call their destructor.
Not necessarily.
If your compiler implements Return Value Optimization (RVO) then it can set up the call to
f()such thatsis constructed where the caller would store the return value, and therefore it can elide the calls to theCStringcopy constructor and destructor. This optimization is one of the few exceptions permitted by the C++ standard to the as-if optimization rule.If you are compiling with all optimizations disabled, you would likely see one or more calls to the
CStringcopy constructor and destructor in processing the call tof().