In your example, that's fine, since you're making a copy of the int
.
If you get the int
as a reference, then after that line, it would be a dangling reference, since the shared pointer would go out of scope, deleting its target.
Is it also a bad idea to use shared_ptr to avoid copying the whole object? Like a vector of matrices/images for example.
Using shared_ptr
will avoid copying just as using a naked pointer will avoid copying - decide whether you want to avoid copying (first), and then choose which sort of pointer you should use.
For a vector of matrices or images, you may want to use a std::vector
of boost::shared_ptr
, or a boost::ptr_vector
, or some other container that makes the memory management easy for you.