I don't think steg90 wants to make a hijacking pointer. It is my understanding he wants normal pointers, except that a delete on them also sets them to NULL.
Of course, you can't catch a delete that happens elsewhere in the program (where other pointers point to the same object) except if the pointer class only points to special classes that will notify it. This is what QPointer does. It can only point to QObject descendants.
So the best you can do is make the pointer NULL after you call delete on that specific pointer. Which makes the class purely a convenience class.
You can't overload the delete operator to take a non-pointer. That is where the compiler error comes from. So a del() function will have to do.
Here is your convenience class:
template<class T>
class Pointer {
public:
Pointer (T* p) {
_ptr = p;
}
void del() {
delete _ptr;
_ptr = NULL;
}
// More operators. Like the dereferencing operators: * and ->
private:
T* _ptr;
};
template<class T>
class Pointer {
public:
Pointer (T* p) {
_ptr = p;
}
void del() {
delete _ptr;
_ptr = NULL;
}
// More operators. Like the dereferencing operators: * and ->
private:
T* _ptr;
};
To copy to clipboard, switch view to plain text mode
I just read your latest post, steg90. Why would you want to give a pointer the value NULL if it falls out of scope anyway?
Bookmarks