I need to pass a pointer to a class, so some code that I do not control. This code automatically releases () s pointer when this is done, but I need a class later. I was hoping that I could just create a wrapper class that would not allow the class to be freed without actually preventing the code from accessing it, but virtual calls do not work.
template <class T> class PointerWrapper:public T { public: T* p; PointerWrapper(T *ptr) { p=ptr; } ~PointerWrapper(void) { } T* operator->() const { return p; } T& operator*() const { return *p; } }; void codeIDontControl(Example *ex) { ex->virtualfunction(); delete ex; } void myCode() { Example *ex=new Example(); codeIDontControl(ex); do something with ex
Basically, ex-> virtualfunction () calls a virtual function in PointerWrapper itself instead of a virtual function in PointerWrapper-> p. It seems like it is ignoring the β operator?
Now I donβt need to use the PointerWrapper-esque class if there is another way to do this, but thatβs all I could think of ...
I can't change the example either, but I can subclass it
source share