I’m writing some Gui code where I have a load of Widget objects managed by ofPtr. I want a method on a widget to be able to set a field on another widget to be an ofPtr to itself. Which left me wanting to get an ofPtr to this, without creating duplicate pointers to the same object with their own ref counts leading to a double free.
Googling for a solution I found std::enable_shared_from_this which does what I want with std:
:shared_ptr, which ofPtr sub classes. So I have:
class Widget;
typedef ofPtr<Widget> WidgetPtr;
class Widget : public tr1::enable_shared_from_this<Widget> {
public:
Widget();
virtual ~Widget() {}
WidgetPtr getPtr() { return (WidgetPtr)shared_from_this(); }
Which fails because it cant convert from a std::tr1::shared_pointer to an ofPtr:
../../../libs/openFrameworks/types/ofTypes.h:102:7: note: no known conversion for argument 1 from std::tr1::shared_ptr<ofxPg::Widget> to const ofPtr<ofxPg::Widget>&
I was hoping this might work as ofPtr is a subclass and I’m starting to get that special pointers headache!
Anyone any ideas how to fix this? If the conversion is possible should we patch ofPtr to make it work?
cheers,
mark