getting a normal ptr from shared_ptr?


Question

I have something like shared_ptr<Type> t(makeSomething(), mem_fun(&Type::deleteMe)) I now need to call C styled function that requires a pointer to Type. How do I get it from shared_ptr?

1
46
4/13/2015 11:31:44 AM

Use the get() method:

boost::shared_ptr<foo> foo_ptr(new foo());
foo *raw_foo = foo_ptr.get();
c_library_function(raw_foo);

Make sure that your shared_ptr doesn't go out of scope before the library function is done with it -- otherwise badness could result, since the library may try to do something with the pointer after it's been deleted. Be especially careful if the library function maintains a copy of the raw pointer after it returns.

132
2/2/2009 10:04:50 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon