For example:
boost::shared_ptr<int> test() {
boost::shared_ptr<int> x(new int(3));
return x;
}
void function() {
int y = *test();
...
}
Is it also a bad idea to use shared_ptr to avoid copying the whole object? Like a vector of matrices/images for example.
In the general case, no. Your example copies the contents of the
shared_ptr, and then the original value is deleted.Now, the bigger issue here is that it’s fantastically inefficient to do a dynamic memory allocation for an
int, but I’m assuming you’re not doing that in real code. 🙂