This will not help. _member will become a dangling pen. This is because auto_ptr guarantees destruction at the end of the scope: no more and no less.
There are 2 possible answers.
- You can create the type _member
boost::shared_ptr<const B> . - Alternatively, if class B is small , copyable , monomorphic , and the object identifier does not need to be saved , you can make _member a value and save a copy of the argument there. This is by far the easiest option, but obviously it is very limited.
In response to your editing: This is indeed the case I spoke of. By returning myA by value, a copy is created, and the _member instance refers to the already destroyed local one. As described, both shared_ptr and value semantics solve this problem.
Iraimbilanja
source share