C++'s implicit copy and move behavior offer an excellent way to shoot yourself in the foot. Here's an example of how they can create a double-free and how to avoid it.
You’d be missing the whole point too if you think that the pointer is the root cause. The problem is that the class is not designed to be copy-able, let alone moveable. Your suggestion to use a unique_ptr will also blow up in your face the moment you try to copy an instance.
Depends on what semantic you want. Sure, if you use a unique_ptr member, you will get a deleted copy constructor/operator - I wouldn’t consider that blowing up in my face.
And even the presented fix hurts my eyes. Should have used a
unique_ptr
oroptional
.Exactly, raw pointers are very rarely the way to go
You’d be missing the whole point too if you think that the pointer is the root cause. The problem is that the class is not designed to be copy-able, let alone moveable. Your suggestion to use a
unique_ptr
will also blow up in your face the moment you try to copy an instance.Depends on what semantic you want. Sure, if you use a
unique_ptr
member, you will get a deleted copy constructor/operator - I wouldn’t consider that blowing up in my face.deleted by creator