As long as you are sure that all rights to use void* transferred to you in f, that void* really refers to object A , and you know the correct way to clear up, then you can just go ahead and use the smart pointer to accept the right ownership:
typedef boost::shared_ptr<A> A_SPtr; void *f(void *var){ A_SPtr instance(static_cast<A*>(var)); }
Edit: this is not clear from the question, but some comments show that you are actually trying to pass a smart pointer through the βcommonβ interface that void* uses. You do this the same way as for any type:
void *f(void *var){ A_SPtr *instance = static_cast<A_SPtr *>(var); }
And you pass a pointer like:
A_SPtr a; f(&a);
Like any pointer, you must make sure that the lifetime of the object is sufficient for the pointer to be valid when f receives it.
source share