[comp.lang.c++] Virtual Destructors?

daveh@cbmvax.UUCP (Dave Haynie) (12/23/88)

I'm pretty new to this C++ game, but I've already run into something that
doesn't make sense.  Perhaps this is already fixed -- I'm using Lattice 
C++ V1.00 on the Amiga, which is based on cfront V1.1a.  In any case, I
can't figure out why in the world destructors aren't virtual.  Basically,
I have this situation that looks pretty much like the following:

	class A {
		...
		A(..) {...}
		~A( ) {...}
		...
	};

	class B : public A {
		...
		B(..) : (...) {...}
		~B( ) {...}
		...
	};

	... // Much later
	
	A tool;

	if (thing)
           tool = new A(...);
	else
	   tool = new B(...);

	... // Yet again even later

	delete tool;

No problem, except that when I delete tool, I always get A's destructor,
not B's.  Certainly I can take care of this problem by having each 
destructor call a virtual function which does the actual destroying, but
I can't think of any good reason for destructors to not be virtual.  Is
this really a problem, or am I missing something really obvious?




-- 
Dave Haynie  "The 32 Bit Guy"     Commodore-Amiga  "The Crew That Never Rests"
   {uunet|pyramid|rutgers}!cbmvax!daveh      PLINK: D-DAVE H     BIX: hazy
              Amiga -- It's not just a job, it's an obsession

ark@alice.UUCP (Andrew Koenig) (12/23/88)

In article <5568@cbmvax.UUCP>, daveh@cbmvax.UUCP (Dave Haynie) writes:
> I'm pretty new to this C++ game, but I've already run into something that
> doesn't make sense.  Perhaps this is already fixed -- I'm using Lattice 
> C++ V1.00 on the Amiga, which is based on cfront V1.1a.  In any case, I
> can't figure out why in the world destructors aren't virtual.

If you want a destructor to be virtual, make it virtual:

	class A {
	public:
		A();
		virtual ~A();
		// and so on
	};

Be aware that there is a fairly widespread bug that causes virtual
destructors not to work quite right unless all derived classes have
explicit destructors, even if the destructors are empty.  So:

	class B: public A {
	public:
		// stuff
		~B() { }		// empty destructor
		// other stuff
	};

But aside from that, virtual destructors work just fine.
-- 
				--Andrew Koenig
				  ark@europa.att.com

smith@ward.UUCP (Tom Smith) (12/28/88)

From article <8590@alice.UUCP>, by ark@alice.UUCP (Andrew Koenig):
> If you want a destructor to be virtual, make it virtual:
> 
    [ example of virtual destructor ]
> 
> Be aware that there is a fairly widespread bug that causes virtual
> destructors not to work quite right unless all derived classes have
> explicit destructors, even if the destructors are empty.

There is also a bug in our translator (Glockenspiel, derived from AT&T)
that could well exist in most, if not all.  It goes thusly:
Ordinarily, the delete operator is defined to "do the correct thing"
when the operand is a pointer of any type set to zero.  However, if the
operand is a pointer to an instance of a class with a virtual destructor,
and the pointer is set to zero, the resulting code tries to dereference
the pointer to call the virtual function without checking for a zero value
first.  Of course, this crashes your application promptly.

    Thomas Smith
    hplabs!analog!smith