[comp.lang.c++] Deleting derived classes

matthew@cs.adelaide.edu.au (Matthew Donaldson) (11/06/90)

Hi.  I have a class derived from another one, and I want to
have full control over how they are deleted.  The base class has a reference
count, and I want to just decrement that count if it is not zero, and
do no deletion of either the base class or the derived one if it is
non-zero.

I have experimented with user-defined new and delete operators for derived
classes, and it seems, at least with AT&T 2.0, that if a delete function
is declared in the derived class, it will be used, and the one defined in
the base class will be ignored.  Is this part of the language definition,
or a bug in the compiler?

The other, maybe related problem relates to virtual functions.  I have a setup
like this:

class Base
{
 int Ref;
 void operator delete(void* Object)
    { if (Ref == 0) ::delete(Object); else Ref--; }
public:
 virtual void X() { Fatal("Class without X function used\n"); }
 Y() { ...; X(); ... }  // X is called from the base class
...
}

class Derived
{
 void X() { do things; }
...
}

I know that I could use abstract classes, but this is how I first wrote the
classes, and I hadn't got around to changing them.

Calling X from the base class works fine, until I try to delete the derived
(and therefore the base) class.  The ref count is initially 2, so
theoretically nothing should be deleted, yet after the deletion, calling X
from the base class gives the "Class without X function" message, indicating
that the base's X function is now being called.  I assume that the virtual
table has been corrupted somehow.  Does it get dismantled before the delete
function of the base class gets control?  Is it possible to stop a deletion
of a derived class like this?

Any help would be much appreciated.

		Thanks in advance,
				   Matthew
--
-------
Matthew Donaldson			_
matthew@cs.ua.oz.au		       / \/
Operating Systems Group		       \_/\
Computer Science Department
University of Adelaide

mjv@objects.mv.com (Michael J. Vilot) (11/10/90)

Matthew Donaldson asks:

> if a delete function
> is declared in the derived class, it will be used, and the one defined in
> the base class will be ignored.  Is this part of the language definition,
> or a bug in the compiler?
This is part of the language definition.  Class-specific operators new and
delete are static, and inherited.  See Sections 5.3.3 and 12.5 of ``The
Annotated C++ Reference Manual.''

> ... [example] ..

> I assume that the virtual
> table has been corrupted somehow.  Does it get dismantled before the delete
> function of the base class gets control?  Is it possible to stop a deletion
> of a derived class like this?
Not corrupted, but changed.  The base class destructor is using the virtual 
table of the base class, not the derived class.  See Section 12.7 of the ARM.

Hope this helps,

--
Mike Vilot,  ObjectWare Inc, Nashua NH
mjv@objects.mv.com  (UUCP:  ...!decvax!zinn!objects!mjv)

rae@gpu.utcs.toronto.edu (Reid Ellis) (11/14/90)

Matthew Donaldson writes:
> if a delete function
> is declared in the derived class, it will be used, and the one defined in
> the base class will be ignored.  Is this part of the language definition,
> or a bug in the compiler?

As previously stated, it is part of the language definition, but note
that this can be circumvented by declaring the destructor as virtual..

struct foo {
	foo();
	virtual ~foo();
};

Now you are assured that the destructors will be called properly.

					Reid

--
Reid Ellis  264 Broadway Avenue, Toronto ON, M4P 1V9               Canada
rae@gpu.utcs.toronto.edu || rae%alias@csri.toronto.edu || +1 416 487 1383