c++ - `shared_ptr<SubClass>` 如何成为 `shared_ptr<BaseClass>` 的子类?

标签 c++

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

我想知道如何实现模板类 C<T>这样 C<SubClass>C<BaseClass> 的子类?

我已经观察到上述情况,例如 shared_ptr<SubClass>shared_ptr<BaseClass> ,在这里我可以将前者分配给后者。

最佳答案

类(class)shared_ptr<SubClass>不是继承自 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.

shared_ptr<BaseClass>有这样的赋值运算符template< class Y > shared_ptr& operator=( const shared_ptr<Y>& r ) noexcept;Y 时进行转换是SubClass .

关于c++ - `shared_ptr<SubClass>` 如何成为 `shared_ptr<BaseClass>` 的子类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47125202/

相关文章:

c++ - 无法使用程序访问文件

c++ - 重新 C++ 范围迭代器 : Is it bad to have just one class?

c++ - 尝试进行多线程时 WriteFile() 崩溃

c++ - 在 C++ 中正确实现 Vector 3

c++ - 返回变量为 void (*)(void)

C++ chrono - 将持续时间作为 float 或 long long

c++ - CUDA C++ : Expected an expression in kernel. cu 文件

c++ - 预期 `__asm` 语句中的表达式

c++ - 为类/信息隐藏创建内部和外部接口(interface)

c++ - 如何使依赖于模板的 typedef 更方便?