Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!wuarchive!psuvax1!rutgers!att!dptg!ulysses!andante!alice!shopiro From: shopiro@alice.UUCP (Jonathan Shopiro) Newsgroups: comp.lang.c++ Subject: Re: Virtual destructors - why aren't they all? Summary: Some classes don't have any virtual functions Message-ID: <10021@alice.UUCP> Date: 14 Oct 89 20:57:05 GMT References: <10627@csli.Stanford.EDU> Organization: AT&T Bell Laboratories, Murray Hill NJ Lines: 19 In article <10627@csli.Stanford.EDU>, keith@csli.Stanford.EDU (Keith Nishihara) writes: - I was surprised to discover that calling delete on a derived class - object pointed to by a pointer to the base class failed to call - the derived class destructor. (cfront 1.2) (See example below.) - I cannot think of any case where this would be the desirable behaviour. - Fortunately, it appears that declaring the destructor virtual causes - the appropriate behaviour to happen. Why are not all desctructors - virtual? Is this true in other C++ systems? It's true at least in C++ 2.0. The reason why is that some classes have no virtual functions, and adding a virtual destructor would cost one word per object of the class. Some classes are not intended to be inherited from, so they don't need any virtual functions. I think it's generally true that if a class has any virtual functions, it should have a virtual destructor, but it's up to you to do it. -- Jonathan Shopiro AT&T Bell Laboratories, Warren, NJ 07060-0908 research!shopiro (201) 580-4229