[comp.lang.c++] destructors with MI

880716a@aucs.uucp (Dave Astels) (06/06/90)

I have a class dag as follows:

class ConsCell {
	ConsCell *next;
// ...
}

class List {
	ConsCell * head, *tail;
public:
	~List ()  ...
// ...
}

class String {
	~String () ...
// ...
}

class ListCell : public ConsCell, public List {
// ...
}

class StringCell : public ConsCell, public String {
// ...
}

A List consists of a linked list of ConCells, pointed to by List::head,
List::tail is simple for makeing appending (a very much used operation)
more effient.  The ConsCells in this list are either ListCells or
StringCells.  Everything works fine except for destructing.

When a ListCell or StringCell gets destructed I want the List/String
destructor to be called.  ListCells & StringCells (and any future kinds
of Cells) are deleted from the List object containing them, thus they
are seen as ConsCells.

My question is: how do I get them to destruct as Lists/Strings when
they are deleted as ConsCells  ????

-- 
- Dave Astels

Internet: 880716a@AcadiaU.CA
Bitnet:   880716a@Acadia

pkturner@cup.portal.com (Prescott K Turner) (06/13/90)

Dave Astels writes:

> My question is: how do I get them to destruct as Lists/Strings when
> they are deleted as ConsCells  ????

If you declare a virtual destructor in class ConsCell, then deleting the
object as a ConsCell will call the destructor for the object's most
derived type, i.e. ListCell or StringCell.  C++ automatically generates
destructors for ListCell and StringCell which call the List/String
destructors, because those are also base classes of ListCell/StringCell
respectively.
--
Prescott K. Turner, Jr.
Language Processors, Inc.
959 Concord St., Framingham, MA 01701 USA    (508) 626-0006 x232
UUCP: ...sun!cup.portal.com!pkturner    Internet: pkturner@cup.portal.com