Ale*_*etu 3 c++ pointers casting shared-ptr
我有一个OpenGL项目的问题,从void*指针转换为shared_ptr<mytype>.
我正在使用Bullet在刚体上设置指针:
root_physics->rigidBody->setUserPointer(&this->root_directory->handle);
Run Code Online (Sandbox Code Playgroud)
手柄属于类型shared_ptr<mytype>.
该void*指针由子弹的库函数返回,getUserPointer():
RayCallback.m_collisionObject->getUserPointer()
Run Code Online (Sandbox Code Playgroud)
要转换回mytype,static_cast不起作用:
std::shared_ptr<disk_node> u_poi = static_cast< std::shared_ptr<disk_node> >( RayCallback.m_collisionObject->getUserPointer() );
Run Code Online (Sandbox Code Playgroud)
错误,在编译时:
/usr/include/c++/4.8/bits/shared_ptr_base.h:739:39: error: invalid conversion from ‘void*’ to ‘mytype*’ [-fpermissive]
Run Code Online (Sandbox Code Playgroud)
知道如何从void*返回的转换getUserPointer()为shared_ptr<mytype>?
由于您正在存储指向实例的指针,因此std::shared_ptr您需要将返回的值转换getUserPointer为std::shared_ptr<>*而不仅仅是std::shared_ptr<>.
std::shared_ptr<disk_node>* u_poi
= static_cast< std::shared_ptr<disk_node>* >(RayCallback.m_collisionObject->getUserPointer());
Run Code Online (Sandbox Code Playgroud)