Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > calling virtual destructors and virtual functions

Reply
Thread Tools

calling virtual destructors and virtual functions

 
 
marcwentink@hotmail.com
Guest
Posts: n/a
 
      01-31-2006
Say I have a class A, and a class B that inherits from A. Now A (and B)
has a virtual destructor and a virtual function F();

If I now make these statements

A* ptrA = new B;
ptrA->F();
delete ptrA

then in the statement ptrA->F(), by means of the polymorph behavior,
the F() of class B is called. But the F() of class B only. And in
delete ptrA, both destructors of class A and B are called.

Is this right? Because then the two statements behave differently.
Somehow, for a moment, I was thinking that in the call ptrA->F(), both
virtual functions of class A and B should be processed.

 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      01-31-2006
* :
> Say I have a class A, and a class B that inherits from A. Now A (and B)
> has a virtual destructor and a virtual function F();
>
> If I now make these statements
>
> A* ptrA = new B;
> ptrA->F();
> delete ptrA
>
> then in the statement ptrA->F(), by means of the polymorph behavior,
> the F() of class B is called. But the F() of class B only. And in
> delete ptrA, both destructors of class A and B are called.
>
> Is this right? Because then the two statements behave differently.
> Somehow, for a moment, I was thinking that in the call ptrA->F(), both
> virtual functions of class A and B should be processed.


A destructor is a _special_ member function. You can think of it as if
the compiler inserts calls to destructors of data members and base
classes at the end of the destructor. If you want that kind of behavior
for other member functions you'll have to code it yourself.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
 
 
 
Bob Hairgrove
Guest
Posts: n/a
 
      01-31-2006
On 31 Jan 2006 08:24:05 -0800, ""
<> wrote:

>Say I have a class A, and a class B that inherits from A. Now A (and B)
>has a virtual destructor and a virtual function F();
>
>If I now make these statements
>
>A* ptrA = new B;
>ptrA->F();
>delete ptrA
>
>then in the statement ptrA->F(), by means of the polymorph behavior,
>the F() of class B is called. But the F() of class B only. And in
>delete ptrA, both destructors of class A and B are called.
>
>Is this right? Because then the two statements behave differently.
>Somehow, for a moment, I was thinking that in the call ptrA->F(), both
>virtual functions of class A and B should be processed.


The destructor is a special function. A is a base class of B,
therefore it is contained by B and has to be destroyed much like any
other member variables of B have to be destroyed when B is destroyed.
If you delete a B* directly, its A part is also deleted -- even when
A's destructor is not virtual.

But the destructor of A (and B) is virtual, so calling delete on an A*
calls B's destructor, and during the normal destruction of B, A's
destructor is also called. It is therefore the same mechanism as
calling an ordinary function; the only difference is that the
destructors are "chained".

--
Bob Hairgrove

 
Reply With Quote
 
Nitin Motgi
Guest
Posts: n/a
 
      01-31-2006
wrote:
> Say I have a class A, and a class B that inherits from A. Now A (and B)
> has a virtual destructor and a virtual function F();
>
> If I now make these statements
>
> A* ptrA = new B;
> ptrA->F();
> delete ptrA
>
> then in the statement ptrA->F(), by means of the polymorph behavior,
> the F() of class B is called. But the F() of class B only. And in


Not untill you the make the F() in A as virtual. If you have it
as virtual you would be calling the F() of B.

> delete ptrA, both destructors of class A and B are called.

Yes because the base class declares the destructor as virtual.

>
> Is this right? Because then the two statements behave differently.
> Somehow, for a moment, I was thinking that in the call ptrA->F(), both
> virtual functions of class A and B should be processed.


No, this is not right. It's only the behaviour of constructors and
destructor
that is different.

-- Nitin Motgi

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Calling virtual functions from destructors? Henrik Goldman C++ 6 11-19-2006 10:01 PM
Question regarding virtual functions/destructors Pravesh C++ 3 06-20-2006 08:36 AM
virtual functions and destructors (newbie) Peter C++ 6 10-26-2005 04:16 AM
virtual destructors for classes only with virtual functions? heted7 C++ 33 05-12-2005 09:26 AM
If declared as virtual in base, its derived version also is virtual. Why not for destructors? qazmlp C++ 7 07-27-2004 03:10 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57