Sign in to follow this  
Axiverse

smart pointer questions

Recommended Posts

I have a question about smart pointers: What if you are passing a dereferenced object in an array? How do you tell the user not to use a smart pointer with that? And if you mix normal pointers with smart pointers, wouldn't it be confusing?

Share this post


Link to post
Share on other sites
Passing a dereferenced object shouldn't work, as there would be a type incompatibility. If you mean passing a pointer to an object in an array, then the smart pointer will probably try to delete it, and either crash the program or corrupt the heap. Don't do that.

This is one reason why smart pointers usually accept pointers as constructor arguments for explicit constructors and do not accept pointers as arguments to the assignment operator. When you can only initialize a smart pointer with a pointer at construction, it limits the amount of accidental damage you can do.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this