Home > Mobile >  c smart pointer c'tor design explaination
c smart pointer c'tor design explaination

Time:10-31

When I read the code:

    DefaultSPStorage() : pointee_(Default()) {}

    DefaultSPStorage(const DefaultSPStorage&) : pointee_(nullptr) {}

    template<class U>
    DefaultSPStorage(const DefaultSPStorage<U>) : pointee_(nullptr) {}

    explicit DefaultSPStorage(const StoredType& p) : pointee_(p) {}

I feel confused on the third templated c'tor.

If anyone can enlighten me the purpose of it, I would be really grateful.

CodePudding user response:

DefaultSPStorage() is a template class, eg:

template<typename T>
class DefaultSPStorage
{
    ...
};

The third templated constructor takes a DefaultSPStorage instance whose template parameter can be different than the instance that is being constructed.

IOW, this allows constructing a DefaultSPStorage<A> using a DefaultSPStorage<B> as input, eg:

DefaultSPStorage<short> a;
DefaultSPStorage<int> b(a);
  • Related