Re: virtual destructor revisted
From: John Harrison (john_andronicus_at_hotmail.com)
Date: 06/20/04
- Next message: Conrad Weyns: "Re: Code or library or compiler bug?"
- Previous message: John Harrison: "Re: question about cin input ?"
- In reply to: ctick: "virtual destructor revisted"
- Next in thread: E. Robert Tisdale: "Re: virtual destructor revisted"
- Reply: E. Robert Tisdale: "Re: virtual destructor revisted"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sun, 20 Jun 2004 06:36:18 +0100
"ctick" <ctick@flare.com> wrote in message
news:bZ8Bc.7621$OB3.2639@bgtnsc05-news.ops.worldnet.att.net...
> A reason for declaring a "virtual destructor" for a Base class is to make
> sure the destructor of Derived class will be invoked when a pointer of
Base
> type is used to delete an object of Derived.
>
> Is this the only reason to define "virtual destructor" for a Base class?
Pretty much yes. There is one other rather obscure reason. It might be that
you want class Base to have at least one virtual function (so that you can
use dynamic_cast on it for instance) but you don't have any other suitable
method to declare as virtual.
>
> Since it's always possible that users of any Base class will delete a
> Derived object like that, does this mean that a "virtual destructor" is
> always necessary?
Not really. There is an overhead to using virtual functions. If your class
is not intended to be used polymorphically then don't use any virtual
functions (including destructor) and document that fact. Unfortunately its
always possible that users will do something stupid.
john
- Next message: Conrad Weyns: "Re: Code or library or compiler bug?"
- Previous message: John Harrison: "Re: question about cin input ?"
- In reply to: ctick: "virtual destructor revisted"
- Next in thread: E. Robert Tisdale: "Re: virtual destructor revisted"
- Reply: E. Robert Tisdale: "Re: virtual destructor revisted"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|