从 C++ 中的派生 class 获取共享指针
Getting shared pointer from this of derived class in C++
我想从派生的 class 中得到 shared_ptr。
std::enable_shared_ptr_from_this
由基础 class 继承并获得 std::shared_ptr<Base>
,但不是 std::shared<Derived>
。我可以使用 std::reinterpret_pointer_cast
或 std::dynamic_pointer_cast
但是
- 这么多代码
- 多重继承安全吗?
或者这是唯一的方法?
示例代码:
class Base : public std::enable_shared_from_this<Base> {
};
class Derived : public Base {
public:
std::shared_ptr<Derived> GetPointer() const {
return shared_from_this(); // but it returns std::shared<Base> type
}
};
提前致谢!
按照您目前的方式进行操作非常安全。
#include <memory>
class Base : public std::enable_shared_from_this<Base>{
public:
virtual int32_t getid() = 0;
};
class Derived : public Base{
public:
int32_t id;
Derived(int32_t id){
this->id = id;
}
~Derived(){
}
int32_t getid(){
return id;
}
};
int main(){
std::shared_ptr<Derived> child = std::make_shared<Derived>(1033);
std::shared_ptr<Base> parent = child;//This needs no extra work
std::shared_ptr<Derived> secondchildinstance = std::static_pointer_cast<Derived>(parent);//this needs casting
if(secondchildinstance!=nullptr){
//shared_from_this can be called directly from the child without an explicit method
std::shared_ptr<Base> secondparentinstance = secondchildinstance->shared_from_this();
//shared_from_this would require downcasting to get the child
std::shared_ptr<Derived> thirdchildinstance = std::static_pointer_cast<Derived>(secondchildinstance->shared_from_this());//requires casting
if(thirdchildinstance!=nullptr){
printf("use count:%ld id:%d",thirdchildinstance.use_count(),thirdchildinstance->getid());
}
}
return 0;
}
您可能可以通过包装方法使 down-casting 从 parent 到 child 的工作更轻松。
需要注意的是,您不需要显式方法来调用 shared_from_this,因为 parent 已经继承了它,您可以调用 child->shared_from_this() 直接但它会给出需要向下转换的基础 class 的共享实例。
我想从派生的 class 中得到 shared_ptr。
std::enable_shared_ptr_from_this
由基础 class 继承并获得 std::shared_ptr<Base>
,但不是 std::shared<Derived>
。我可以使用 std::reinterpret_pointer_cast
或 std::dynamic_pointer_cast
但是
- 这么多代码
- 多重继承安全吗?
或者这是唯一的方法?
示例代码:
class Base : public std::enable_shared_from_this<Base> {
};
class Derived : public Base {
public:
std::shared_ptr<Derived> GetPointer() const {
return shared_from_this(); // but it returns std::shared<Base> type
}
};
提前致谢!
按照您目前的方式进行操作非常安全。
#include <memory>
class Base : public std::enable_shared_from_this<Base>{
public:
virtual int32_t getid() = 0;
};
class Derived : public Base{
public:
int32_t id;
Derived(int32_t id){
this->id = id;
}
~Derived(){
}
int32_t getid(){
return id;
}
};
int main(){
std::shared_ptr<Derived> child = std::make_shared<Derived>(1033);
std::shared_ptr<Base> parent = child;//This needs no extra work
std::shared_ptr<Derived> secondchildinstance = std::static_pointer_cast<Derived>(parent);//this needs casting
if(secondchildinstance!=nullptr){
//shared_from_this can be called directly from the child without an explicit method
std::shared_ptr<Base> secondparentinstance = secondchildinstance->shared_from_this();
//shared_from_this would require downcasting to get the child
std::shared_ptr<Derived> thirdchildinstance = std::static_pointer_cast<Derived>(secondchildinstance->shared_from_this());//requires casting
if(thirdchildinstance!=nullptr){
printf("use count:%ld id:%d",thirdchildinstance.use_count(),thirdchildinstance->getid());
}
}
return 0;
}
您可能可以通过包装方法使 down-casting 从 parent 到 child 的工作更轻松。
需要注意的是,您不需要显式方法来调用 shared_from_this,因为 parent 已经继承了它,您可以调用 child->shared_from_this() 直接但它会给出需要向下转换的基础 class 的共享实例。