Is it ok to do the following? Obviously the caller is not prevented from deleting the naked pointer. Should I use shared_ptr and return a shared_ptr? It seems too heavy for this.
class A
{
B* GetB() { return pointer.get(); }
private:
unique_ptr<B> pointer;
};
I think that it should work fine as it is, making it clear in the documentation that such pointer is not intended to be
deleted.Still, I would probably return a reference: in that case it’s implicit that the caller mustn’tdeleteanything.Edit it turns out that the pointer can be
NULL; in such case, the reference must be avoided.You could create some kind of smart-dumb
do_not_delete_me_ptrclass that just encapsulates the pointer and make extra clear that you must not delete it, but I think this too is overkill. 😉