From: Andrew G. <ag...@em...> - 2003-01-07 01:22:59
|
On 02 Jan 2003 02:20:22 -0500, Michael D. Crawford wrote: > In building ZooLib under project builder (with g++) I get a lot of warning > messages about classes that have some virtual functions but don't have virtual > destructors. ZMutexBase is one, I think there are others. > > That is probably not what we want. > > If you have a non-virtual destructor and you delete an object whose real type > is a derived type, by deleting a pointer of the base type, you will call the > base class' destructor but not the derived class' destructor. > > That would be OK if you never had any derived classes. It is common for STL > templates to not have virtual destructors. They are not meant to be used as > base classes. > > If you have some virtual member functions in a class, that suggests that this > is meant to be a base class. If you have a pointer of base type that really > points to an object of derived type, and call one of the virtual functions > through the pointer, you will get a call to the derived class' version of the > function, if there is one. > > Generally you either want no virtual functions at all (in which case you > shouldn't inherit from the class) or if you do intend this to be a base class, > the destructor should be virtual. In general this is correct. However in this case ZMutexBase is intended not as a base class for implementation-inheritance, but as an interface definition. The indication that this is the case is that the destructor and constructor are protected, and copy-constructor and assignment are private. You cannot use any aspect of a ZMutexBase *other* than it's virtual methods -- you can't create, assign, copy from or delete with a ZMutexBase reference or pointer. A+ -- Andrew Green mailto:ag...@em... Electric Magic Co. Vox/Fax: +1 (408) 907 2101 |