I like std::unique_ptr. It helps me out to prevent memory leaks, which is extremely useful. But there's one problem: copy assignment and construction is not allowed.
Even though this restriction serves safety of a programmer, it is quite limiting, too. If you work with classes with std::unique_ptr as their member(s) using copy assignment and construction, you end up having problems. That's why I created my own wrapper around unique_ptr with copy construction and assignment. Here's it's copy constructor:
template<typename T, class Deleter>
PointerSmartSafe<T, Deleter>::PointerSmartSafe(PointerSmartSafe const& pointer_smart_safe_) noexcept : _m_opPointerUnique((_m_opPointerUnique == nullptr) ? new T(*_m_opPointerUnique.get()) : nullptr){
}
And here's the copy assignment operator:
template<typename T, class Deleter>
PointerSmartSafe<T, Deleter>& PointerSmartSafe<T, Deleter>::operator=(PointerSmartSafe const& pointer_smart_safe_) noexcept{
_m_opPointerUnique = decltype(_m_opPointerUnique)(new T(*_m_opPointerUnique.get()));
return *this;
}
Everything worked fine until I ended up using abstract base class as a type (T). I got an error message like the following:
error: cannot allocate an object of abstract type 'AbstractBaseClass'
This perplexes me. Does there exist a workaround?