|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Hello everybody.
I have a question. When i want use a pure virtual destructor in one of my classe, i must give a definition of this destructor like this: class A { //a lot of things virtual ~A() =0; }; A::~A() { //.... } However,i ask myself why ? Does anyone have one reponse ? Thanks. David Côme. |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
David Côme wrote:
> Hello everybody. > I have a question. > When i want use a pure virtual destructor in one of my classe, i must > give a definition of this destructor like this: > > class A > { > //a lot of things > virtual ~A() =0; > }; > > A::~A() > { > //.... > } > > However,i ask myself why ? > Does anyone have one reponse ? If you ever derive from your class A, an instance of it will have to be destructed at some point, most likely (unless all you do is dynamically allocate the derived classes and never deallocate them in your program). If your destructor is pure and not implemented, then an attempt to destruct an instance of 'A' will result in a call to a pure virtual function, which has undefined behaviour. To avoid undefined behaviour you need to provide the implementation for A::~A. That's one response I have. V -- Please remove capital 'A's when replying by e-mail I do not respond to top-posted replies, please don't ask |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
On Mon, 24 Dec 2007 19:00:31 +0100, Victor Bazarov
<v.Abazarov@comacast.net> wrote: > If you ever derive from your class A, an instance of it will have > to be destructed at some point, most likely (unless all you do is > dynamically allocate the derived classes and never deallocate them > in your program). If your destructor is pure and not implemented, > then an attempt to destruct an instance of 'A' will result in > a call to a pure virtual function, which has undefined behaviour. > To avoid undefined behaviour you need to provide the implementation > for A::~A. That's one response I have. > > V Thanks |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
On Dec 24, 1:00 pm, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
> David Côme wrote: > > Hello everybody. > > I have a question. > > When i want use a pure virtual destructor in one of my classe, i must > > give a definition of this destructor like this: > > > class A > > { > > //a lot of things > > virtual ~A() =0; > > }; > > > A::~A() > > { > > //.... > > } > > > However,i ask myself why ? > > Does anyone have one reponse ? > > If you ever derive from your class A, an instance of it will have > to be destructed at some point, most likely (unless all you do is > dynamically allocate the derived classes and never deallocate them > in your program). If your destructor is pure and not implemented, > then an attempt to destruct an instance of 'A' will result in > a call to a pure virtual function, which has undefined behaviour. > To avoid undefined behaviour you need to provide the implementation > for A::~A. Actually, looks like he already has the implementation defined. He simply needs to remove '= 0' from the declaration. So, there's really two problems: the first is trying to make the destructor pure virtual, and the second is trying to define the destructor in the base class after having made it pure virtual!! |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
On 2007-12-25 23:07:50 -0600, johanatan <johanatan@gmail.com> said:
> > Actually, looks like he already has the implementation defined. He > simply needs to remove '= 0' from the declaration. So, there's really > two problems: the first is trying to make the destructor pure virtual, > and the second is trying to define the destructor in the base class > after having made it pure virtual!! A pure virtual destructor serves a different need than the typical pure virtual function. The syntax exists to allow you to define a virtual destructor, and still have the class treated as abstract. This is useful in certain cases where you want to define an abstract interface or tag class through which a derived object can be deleted. Without the =0 in the destructor, you'd have to define another pure virtual function in order to make the base class abstract. For instance, class Tag { public: virtual ~Tag() = 0; }; Tag::~Tag() {} class Foo: public Tag { public: Foo(); virtual ~Foo(); }; Foo::Foo() {} int main() { //Tag t; // <-- error, abstract. Tag* pt = new Foo(); // <-- ok. delete pt; // <-- ok, Foo::~Foo is called. } -dr |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
johanatan wrote:
> So, there's really two problems: the first is trying to make the > destructor pure virtual, and the second is trying to define the destructor > in the base class after having made it pure virtual!! No, there is no problem. Both are valid. In fact, you _must_ implement a pure virtual destructor. |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
On Dec 26, 12:35 am, Dave Rahardja
<drahardja.place...@sign.here.pobox.com> wrote: > On 2007-12-25 23:07:50 -0600, johanatan <johana...@gmail.com> said: > > > > > Actually, looks like he already has the implementation defined. He > > simply needs to remove '= 0' from the declaration. So, there's really > > two problems: the first is trying to make the destructor pure virtual, > > and the second is trying to define the destructor in the base class > > after having made it pure virtual!! > > A pure virtual destructor serves a different need than the typical pure > virtual function. The syntax exists to allow you to define a virtual > destructor, and still have the class treated as abstract. This is > useful in certain cases where you want to define an abstract interface > or tag class through which a derived object can be deleted. Without the > =0 in the destructor, you'd have to define another pure virtual > function in order to make the base class abstract. > Ahh, Ok. Thanks for the clarification. This is definitely one of the least intuitive parts of C++ I've ever encountered. Guess you learn something new each day! |
|
![]() |
| Outils de la discussion | |
|
|