[smart pointer] need .get() to access derived class method?
Hi All, I am trying to wrap my head around using shared pointer, but I find the documentation hard to follow, not being a professional programmer. I have a base class, from which many classes are derived and I hold the derived class instance as a point of type baseclass. Usually, I only need access to the base class methods, but now i need to access some of the derived class's methods. How do I do that from a shared pointer? I now have some mix of shared_pointer and "old style" pointer logic in my code, what am I missing? --- _pcStim = boost::shared_ptr<CStim>(new CStimUniform2D()); // create derived instance and store as base CStim* pCStimU = _pcStim.get(); // get raw pointer ((boost::shared_ptr<CStimUniform2D>)(_pcStim))->setNPointGround()=300; // this does not work, what do I need here? ((CStimUniform2D*)pCStimU)->SetPointColor()=_pRoomData->dotColor(); // casting the raw pointer I am fine. --- Best regards and thank you for your reply, Diederick Niehorster
On Sun, Jan 24, 2010 at 11:56 PM, Diederick C. Niehorster <dcnieho@gmail.com> wrote:
I have a base class, from which many classes are derived and I hold the derived class instance as a point of type baseclass. Usually, I only need access to the base class methods, but now i need to access some of the derived class's methods. How do I do that from a shared pointer? _pcStim = boost::shared_ptr<CStim>(new CStimUniform2D());
((boost::shared_ptr<CStimUniform2D>)(_pcStim))->setNPointGround()=300; ((CStimUniform2D*)pCStimU)->SetPointColor()=_pRoomData->dotColor();
In both cases above you use C-style casts, which is a bad idea because they are unsafe. In C++, you use the new (dynamic|static|const)_cast<T> operators instead. Boost extended this idea by providing additional casts: the (dynamic|static|const)_pointer_cast<T> template methods perform these casts, and specializations exists for shared_ptr (as listed in the API doc). boost::shared_ptr<CStimUniform2D> pDerived = boost::dynamic_pointer_cast<CStimUniform2D>(_pcStim); Provided the cast you perform on the shared_ptr using *_pointer_cast<T> is valid on the equivalent source and destination raw pointers using *_cast<T*>, the boost pointer cast is also valid, and will behave similarly. --DD
Hi Dominique, That seems to have done the trick. Thank you for your lesson in pointers, casting and boost's version of it. I'll refer back to it and hope some others might find it useful as well. Best, Dee On Mon, Jan 25, 2010 at 2:46 PM, Dominique Devienne <ddevienne@gmail.com> wrote:
On Sun, Jan 24, 2010 at 11:56 PM, Diederick C. Niehorster <dcnieho@gmail.com> wrote:
I have a base class, from which many classes are derived and I hold the derived class instance as a point of type baseclass. Usually, I only need access to the base class methods, but now i need to access some of the derived class's methods. How do I do that from a shared pointer? _pcStim = boost::shared_ptr<CStim>(new CStimUniform2D());
((boost::shared_ptr<CStimUniform2D>)(_pcStim))->setNPointGround()=300; ((CStimUniform2D*)pCStimU)->SetPointColor()=_pRoomData->dotColor();
In both cases above you use C-style casts, which is a bad idea because they are unsafe. In C++, you use the new (dynamic|static|const)_cast<T> operators instead. Boost extended this idea by providing additional casts: the (dynamic|static|const)_pointer_cast<T> template methods perform these casts, and specializations exists for shared_ptr (as listed in the API doc).
boost::shared_ptr<CStimUniform2D> pDerived = boost::dynamic_pointer_cast<CStimUniform2D>(_pcStim);
Provided the cast you perform on the shared_ptr using *_pointer_cast<T> is valid on the equivalent source and destination raw pointers using *_cast<T*>, the boost pointer cast is also valid, and will behave similarly. --DD _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
On Mon, Jan 25, 2010 at 8:47 AM, Diederick C. Niehorster <dcnieho@gmail.com> wrote:
That seems to have done the trick. Thank you for your lesson in pointers, casting and boost's version of it.
Glad to help. It's on this list I learned about them, not too long ago. I'm now using them quite a bit in our code, and they were indeed confusing for many developers in our team at first. Once you understand they're just like the normal C++ casts, but for smart pointers in general, it becomes obvious. It's an extensible mechanism too, you can specialize the boot pointer cast templates for your own smart pointers, although these days such home grown smart pointers tend to be legacy (and buggy typically...) Be sure to check out http://www.boost.org/doc/libs/1_41_0/libs/smart_ptr/sp_techniques.html for interesting things you can do with shared_ptr, if you want to dig further. --DD
participants (2)
-
Diederick C. Niehorster
-
Dominique Devienne