Shuzheng
Shuzheng

Reputation: 13996

How can `shared_ptr<SubClass>` be a subclass of `shared_ptr<BaseClass>`?

How can shared_ptr<SubClass> be a subclass of shared_ptr<BaseClass>?

I wonder how to implement a template class C<T> such that C<SubClass> is a subclass of C<BaseClass>?

I've observed the above with, e.g., shared_ptr<SubClass> and shared_ptr<BaseClass>, where I can assign the former to the latter.

Upvotes: 0

Views: 110

Answers (1)

&#214;&#246; Tiib
&#214;&#246; Tiib

Reputation: 11014

The class shared_ptr<SubClass> is not inherited from shared_ptr<BaseClass>.

I've observed the above with, e.g., shared_ptr<SubClass> and shared_ptr<BaseClass>, where I can assign the former to the latter.

The shared_ptr<BaseClass> has such assignment operator template< class Y > shared_ptr& operator=( const shared_ptr<Y>& r ) noexcept; that does the conversion when Y is SubClass.

Upvotes: 5

Related Questions