传统的 C++ 代码有时可能会希望执行 return this;(返回一个 CRType* 原始指针)。

为了改为返回 std::shared_ptr<CRType>,你可以让你的类继承自 std::enable_shared_from_this<CRType>(这样一个内部的 std::shared_ptr<> 会开始拥有这个对象),然后用 shared_from_this() 替代 this 进行返回。

Last modified: Tuesday, 28 January 2025, 1:02 AM