I thought that protected members in a base class are accessible from a derived class, but not so:
template <typename T>
class SharedPointer
{
protected:
explicit SharedPointer(T* const ptr) : ptr(ptr) {}
public:
constexpr SharedPointer() : ptr(nullptr) { }
protected:
T* ptr;
};
template <typename T>
class WeakPointer : public SharedPointer<T>
{
protected:
using parent_t = SharedPointer<T>;
WeakPointer(T* ptr) : parent_t(ptr) { }
public:
WeakPointer(const WeakPointer& other)
{
}
WeakPointer(WeakPointer&& other) = delete;
WeakPointer(const SharedPointer<T>& other)
{
this->ptr = other.ptr;
}
};
int main()
{
SharedPointer<int> sp;
WeakPointer<int> wp = sp;
}
The error I get is:
'SharedPointer<int>::ptr': cannot access protected member declared in class 'SharedPointer<int>'
What is the problem here?