Pete Bartlett wrote:
I have taken over some code that boils down to
///begin code
#include <iostream> #include
struct base { int i; base(int i_):i(i_) {}> };
struct derived : base { derived(int i): base(i) {} };
int main() { boost::shared_ptr<derived> pd(new derived(5));
void* ptr_to_pd = &pd;
boost::shared_ptr<base>* ptr_to_pb = static_cast< boost::shared_ptr<base>* >(ptr_to_pd);
Why not: boost::shared_ptr<base> pb = static_pointer_cast<base>(pd);
std::cout << "\n" << pb->i ; //Prints 5
} ///End code
Now shared_ptr<B> and shared_ptr<A> are unrelated types, so IIUC there is no guarantee that this will work. Is that right? I know from the shared_ptr documentation that I should refactor in the direction of dynamic_ptr_cast (though complications not shown in the code above make that harder that). However for the time being this code appears to be working. Is that just a fluke? I.e. I am relying on the author of boost::shared_ptr and/or my compiler creators not to do something such that it doesn’t work. Is there the possibility that any memory could be deleted twice? Or I am worrying over nothing and in fact there is a reason why the above is safe?
See http://www.boost.org/libs/smart_ptr/shared_ptr.htm#functions. Jeff