Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to cast void* to shared_ptr<mytype>

I have a problem with an OpenGL project, converting from a void* pointer to a shared_ptr<mytype>.

I am using Bullet to set pointers on the rigid body with:

root_physics->rigidBody->setUserPointer(&this->root_directory->handle);

The handle is of type shared_ptr<mytype>.

The void* pointer is returned by Bullet's library function, getUserPointer():

RayCallback.m_collisionObject->getUserPointer()

To convert back to mytype, static_cast is not working:

std::shared_ptr<disk_node> u_poi = static_cast< std::shared_ptr<disk_node> >( RayCallback.m_collisionObject->getUserPointer() );

The error, at compilation time:

/usr/include/c++/4.8/bits/shared_ptr_base.h:739:39: error: invalid conversion from ‘void*’ to ‘mytype*’ [-fpermissive]

Any idea how I can convert from the void* returned by getUserPointer() to shared_ptr<mytype>?

like image 262
Alexandru Staetu Avatar asked Apr 21 '14 19:04

Alexandru Staetu


1 Answers

Since you are storing a pointer to an instance of std::shared_ptr you need to cast the value returned by getUserPointer to std::shared_ptr<>* instead of just std::shared_ptr<>.

std::shared_ptr<disk_node>* u_poi
  = static_cast< std::shared_ptr<disk_node>* >(RayCallback.m_collisionObject->getUserPointer());
like image 139
Captain Obvlious Avatar answered Oct 20 '22 00:10

Captain Obvlious