473,320 Members | 2,083 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

calling virtual destructors and virtual functions

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.

Jan 31 '06 #1
3 1900
* ma*********@hotmail.com:
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?
Jan 31 '06 #2
On 31 Jan 2006 08:24:05 -0800, "ma*********@hotmail.com"
<ma*********@hotmail.com> 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
No**********@Home.com
Jan 31 '06 #3
ma*********@hotmail.com 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

Jan 31 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Chunhui Han | last post by:
Hi, I was recently reading about virtual base classes in C++. The book I was reading says that it is illegal to have non-virtual destructor for the virtual base class. It seems to me that...
6
by: BigMan | last post by:
Is it safe to call nonvirtual member functions from ctors and dtors? What about virtual ones?
23
by: heted7 | last post by:
Hi, Most of the books on C++ say something like this: "A virtual destructor should be defined if the class contains at least one virtual member function." My question is: why is it only for...
11
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining...
37
by: WittyGuy | last post by:
Hi, I wonder the necessity of constructor and destructor in a Abstract Class? Is it really needed? ? Wg http://www.gotw.ca/resources/clcm.htm for info about ]
3
by: Pravesh | last post by:
Hello All, I had some query regarding virtual functions/destructors. If a class is having some/all of its methods that are virtual then is it recommended that it should also have virtual...
6
by: Henrik Goldman | last post by:
Hi I've had a problem with gcc mac osx which I think I figured out. I would like to double check with people here to see if my understanding is correct: I have a class A which class B inherit...
5
by: druberego | last post by:
I read google and tried to find the solution myself. YES I do know that you can get undefined references if you: a) forget to implement the code for a prototype/header file item, or b) you forget...
9
by: desktop | last post by:
On this page: http://www.eptacom.net/pubblicazioni/pub_eng/mdisp.html Shape specify the virtual function: virtual double Intersect( const Shape& s) = 0; then the derived class Circle...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.