
I have taken over some code that boils down to ///begin code #include <iostream> #include <boost/shared_ptr.hpp> 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); std::cout << "\n" << (**ptr_to_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? Many thanks for any advice you can provide, Peter Bartlett