I'm implementing a smart pointer class, and having a couple of confusions. Would really appreciate if people could help me clarify.
1: I thought smart pointer class should have "new" in constructor, "delete" in desctructor. But I can't seem to find a place to put in "new"... So the user will be responsible to create new, while the smart pointer class helps to clean it up?
2: In designing copy assignment operator, a popular approach was to copy-n-swap in order to be thread-safe. However, copy-n-swap requires the object is passed in by value (not by reference). Can it still be used in designing a smart pointer? My concern was that this is a pointer class, hence may not be able to pass in by value. But I'm not very sure about this.
3: If I'm requested to code a smart pointer at interview, do I have to provide some type of reference count? Thought reference count is specific to the shared_ptr...
4: This must be a dumb question, but better ask then keep doubts inside. for SmartPointer<T>& sp to access ptr, should it use sp->ptr or sp.ptr??
Appreciate your input.
template <class T>
class SmartPointer{
T* ptr;
public:
SmartPointer():ptr(NULL){}
SmartPointer(const T& p):ptr(p){}
SmartPointer(const SmartPointer<T>& sp):ptr(sp->ptr){}
SmartPointer<T>& operator=(const SmartPointer<T>& sp);
~SmartPointer(){delete ptr;}
T& operator*(){return *ptr;}
T* operator->(){return ptr;}
};
SmartPointer& operator=(const SmartPointer& sp){
T* pOrig = ptr;
ptr = new T(sp.ptr);
delete pOrig;
return *this;
}
Yes, typically this is what is done. Example:
One of the reasons the construction (and allocation) is left to the client is because T could have all kinds of parameterized constructors. It would be tricky, to say the least, and probably involve variadic constructor templates with some really fancy template magic if you wanted to allow a smart pointer to construct T on its own with all possibilities. It's simpler just to let the client pass in the pointer on constructing the smart pointer. As long as they do that right away on construction, it's safe, since if
operator newthrows before we get to the smart pointer construction, nothing will be allocated/constructed, and so there will be nothing to clean up except what's already on the call stack (which should conform to RAII for exception safety).If you want to make it robust for API boundaries, then you usually want to capture a "deleter" which will call 'operator delete' at the same site in which the code was generated (important when working across module boundaries where trying to free memory in module B for memory allocated in module A would yield undefined behavior).
For this kind of smart pointer you are making which implements no reference counting, typically the best design is to disallow copying (both assignment and copy ctor, though move ctors are fine).
Otherwise you're going back to the archaic kind of practices of transferring ownership (as is the case with the ancient
std::auto_ptr) or trying to deep copy the pointee.Transferring ownership can be especially prone to human error, since it treats the source it's copying from as mutable (which is completely unusual and trippy behavior). If you do that though, you can use atomic CAS to swap pointers, but you need to make the copy constructor and assignment operator accept things by reference, not const reference or by value, since it's going to treat the source as mutable (either that or make the private
ptrmember you have mutable and use const reference).Deep copying the pointee is a kind of interesting idea, but one problem with that is that someone may try to copy your smart pointer at a site where T is not a complete type (not defined, only declared). It's similar to the problem with destructors, so if you want to make a deep copying smart pointer like this, a robust solution would be to capture the copy constructor of T (ex: store a function pointer to a function you generate when your smart pointer is constructed to clone/copy construct new elements of T).
Also slight fix for your existing deep-copying code:
Let the compiler fix your doubts. Given a reference,
SmartPointer& sp,sp->ptrwould be a compiler error in that case unlessThas a member calledptryou can access, which is probably not what you want anyway.sp->ptrwould call your overloadedoperator->, not access the actual private member of the smart pointer.operator->is only defined normally for pointers, so trying to use it on a reference or const reference would invoke your overloaded, user-defined operators.