common interview question.
This mega-pattern is present in 90% of java and c# classes, and also very common in c++. Important
data structure classes relying on pointer fields include vectors, strings, hashtables and most STL or boost containers.
Three common choices for a pointer member variable:
- Raw pointer — default choice
- shared_ptr — often a superior choice
- char pointer, usually a c-str
In each case, we worry about
- construction of this field
- freeing heap memory
- what if the pointee is not on heap?
- copy control of this field
- ownership of the pointer
- when to return raw ptr and when to return a smart ptr
shared_ptr field offers exception safety, automatic delete, default synthesized dtor, copier, op=