Reputation: 322
A library requires binary data to be shared as void *
.
The data to be shared is available as shared_ptr<T>
.
Is there a way to cast shared_ptr<T>
to void *
?
PS: Static casting does not work:
error: invalid static_cast from type ‘std::shared_ptr<DataPacket>’ to type ‘void*’
static_cast<void *>(binData);
Upvotes: 3
Views: 4878
Reputation:
You're going about this wrong. Your problem is not "I need to interpret a shared_ptr<T>
as a void*
" — your problem is "I have a smart pointer to an object, and I need a dumb pointer to that object".
shared_ptr<T>
has a function that does exactly that.
shared_ptr<T> smart;
// ... some code here points smart at an object ...
T *dumb1 = smart.get(); // creates a dumb pointer to the object managed by smart
void *dumb2 = smart.get(); // dumb pointers automatically convert to void*
Do be careful to note that the dumb pointer this creates does not participate in the shared ownership scheme, so you have to take care to ensure the lifetime of the object lasts as long as you need it to.
Upvotes: 6