Claudius Turner Claudius Turner - 3 years ago 67
C++ Question

can't cast to void *

class MyClass {
public:
register_callback(int, void*);
}

typedef boost::shared_ptr<MyClass> myClass_p;

class MyOtherClass {
public:
registerItem(std::pair<std::string, myClass_p>insertItem) {
auto foo = insertItem.second;
void *bar = static_cast<void*>(foo);
}


Given the above code why am I getting "static_cast: cannot convert from myClass_p to void *. I thought you could cast any pointer to a void *.

Answer Source

foo isn't a pointer. It's an object. That object happens to be a boost::shared_ptr<MyClass> and so semantically a smart pointer. But it isn't a pointer.

Assuming the dubious hypothesis that what you're doing makes sense you need:

void *bar = static_cast<void*>(foo.get());

boost::shared_ptr<MyClass>::get() returns the shared object (or 0 if empty).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download