calling virtual destructors and virtual functions

M

marcwentink

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

Alf P. Steinbach

* (e-mail address removed):
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.
 
B

Bob Hairgrove

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".
 
N

Nitin Motgi

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,212
Latest member
BrennaCaba

Latest Threads

Top