Topic: Overloading delete in derived class
Author: ren@math.ohio-state.edu (lmr)
Date: 1995/06/16 Raw View
(2) Suppose I have two classes in a short program:
class A{
A(){};
void operator delete(void* pr){cout<<"Ade"<<endl; }
};
class B:A{
A();
void operator delete(void* pr){cout<<"Bde"<<endl; }
};
main(){
B* ptr=new B;
delete B;
}
My question is about how delete works on ptr.
Does "delete B" also call "delete A"? What is the standard?
I tried using my g++ from linux. It gives me an elegant answer:
test.c: In method `B::B()':
test.c:13: Internal compiler error 241.
test.c:13: Please submit a full bug report to `bug-g++@prep.ai.mit.edu'.
VC++2.0 seems called A::delete after it called B::deleted.
Many thanks.
--
Liming Ren |
Dept. of Mathematics | 231 West 18th Avenue
The Ohio State University | Columbus, Ohio43210
Author: kuehl@uzwil (Dietmar Kuehl)
Date: 1995/06/16 Raw View
lmr (ren@math.ohio-state.edu) wrote:
: (2) Suppose I have two classes in a short program:
: class A{
: A(){};
: void operator delete(void* pr){cout<<"Ade"<<endl; }
: };
:
: class B:A{
: A();
: void operator delete(void* pr){cout<<"Bde"<<endl; }
: };
:
: main(){
: B* ptr=new B;
: delete B;
: }
:
: My question is about how delete works on ptr.
: Does "delete B" also call "delete A"? What is the standard?
No, of course not! You are confusing two concepts here: there is no
need to call two delete functions! Only and at most one delete function
has to be called (accept that a delete operator is allowed to
explicitly call some other delete operator). It has to release the
memory obtained by new. In the case of destructors (A::~A() and
B::~B()) it is necessary that both are called.
: I tried using my g++ from linux. It gives me an elegant answer:
:
: test.c: In method `B::B()':
: test.c:13: Internal compiler error 241.
: test.c:13: Please submit a full bug report to `bug-g++@prep.ai.mit.edu'.
I guess you have already submitted this bug to the given address
(together with the version of you gnu compiler and a description of the
platform used...).
: VC++2.0 seems called A::delete after it called B::deleted.
This is definitely incorrect! This would mean that memory is released
twice. In your example no memory is release but this is due to your
implementation. The operator delete is intended to be used to return
memory to the free store. It is the reverse function of operator new.
Thus you typically implement both or non of the these two operators.
dk
--
http://www.informatik.uni-konstanz.de/~kuehl
dietmar.kuehl@uni-konstanz.de
I am a realistic optimist - that's why I appear to be slightly pessimistic