This is the mail archive of the
egcs-bugs@egcs.cygnus.com
mailing list for the EGCS project.
Wrong delete being called.
- To: Mumit Khan <khan@xraylith.wisc.edu>,egcs-bugs@egcs.cygnus.com
- Subject: Wrong delete being called.
- From: "K. Haley" <khaley@bigfoot.com>
- Date: Thu, 15 Jul 1999 00:06:16 -0600
I have a base class with operator delete declared private. The destructor
of a derived class tries to delete a pointer held by the base class but it
can't. The compiler reports that operator delete is private which is true,
however it is not the delete that should be called. Here is a test case:
#include <new.h>
template<class T> class TPointerBase {
public:
protected:
TPointerBase(T* pointer) : P(pointer) {}
TPointerBase() : P(0) {}
T* P;
private:
void* operator new(size_t) {return 0;} // prohibit use of new
void operator delete(void* p) {((TPointerBase<T>*)p)->P=0;}
};
template<class T> class TPointer : public TPointerBase<T> {
public:
TPointer() : TPointerBase<T>() {}
TPointer(T* pointer) : TPointerBase<T>(pointer) {}
~TPointer() {delete P;}
TPointer<T>& operator =(T* src) {delete P; P = src; return *this;}
T* operator->() {return P;} // Could throw exception if P==0
};
int main()
{
TPointer<int> pi;
return 0;
}
---
Just another brain dead computer user.
Kenneth Haley <mailto:khaley@bigfoot.com>
My website if your interested http://www.bigfoot.com/~khaley