base class virtual destructor not resolving inherited virtual functions

I have a heavy fog over my brain, and can't see the obvious answer...

I have a base class "CBase" and an inherited class "CChild".  I instantiate 
an object of "CChild", work with it, and then delete it.  It has an empty 
destructor, which does nothing, and then calls the CBase destructor, which 
calls a virtual function which is overridden in CChild -- BUT, it doesn't 
call the overriden function (it is simply calling the base class function).

What am I not seeing here?
Thanks
DanB


----- Code snippets -----
class CBase
{
 CBase()                 { }
 virtual ~CBase();
 virtual void MyFunction();
};

CBase::~CBase()
{
 MyFunction();
}

CBase::MyFunction()
{
}

class CChild : CBase
{
 CChild();
 virtual ~CChild();
 virtual void MyFunction();
}

CChild::~CChild()
{
}

void CChild::MyFunction()
{
// does NOT get called from CBase::~CBase()
}

.... code causing the problem...
CChild* p = new CChild();
delete p;


0
Dan
8/18/2006 7:03:06 PM
vc.mfc 33608 articles. 0 followers. Follow

6 Replies
387 Views

Similar Articles

[PageSpeed] 38

On Fri, 18 Aug 2006 13:03:06 -0600, "Dan Baker" <dbmail> wrote:

>I have a heavy fog over my brain, and can't see the obvious answer...
>
>I have a base class "CBase" and an inherited class "CChild".  I instantiate 
>an object of "CChild", work with it, and then delete it.  It has an empty 
>destructor, which does nothing, and then calls the CBase destructor, which 
>calls a virtual function which is overridden in CChild -- BUT, it doesn't 
>call the overriden function (it is simply calling the base class function).
>
>What am I not seeing here?

In C++, virtual functions called from ctors and dtors never resolve to
versions overridden by derived classes, because the derived parts don't
exist at those times. C++ is unlike (and superior to) Java and C# in this
respect.

-- 
Doug Harrison
Visual C++ MVP
0
dsh (2499)
8/18/2006 8:06:31 PM
"Doug Harrison [MVP]" <dsh@mvps.org> wrote in message 
news:a47ce2tot9jk90fondglsa9o1covgm5gv0@4ax.com...
> On Fri, 18 Aug 2006 13:03:06 -0600, "Dan Baker" <dbmail> wrote:
>
>>I have a heavy fog over my brain, and can't see the obvious answer...
>>
>>I have a base class "CBase" and an inherited class "CChild".  I 
>>instantiate
>>an object of "CChild", work with it, and then delete it.  It has an empty
>>destructor, which does nothing, and then calls the CBase destructor, which
>>calls a virtual function which is overridden in CChild -- BUT, it doesn't
>>call the overriden function (it is simply calling the base class 
>>function).
>>
>>What am I not seeing here?
>
> In C++, virtual functions called from ctors and dtors never resolve to
> versions overridden by derived classes, because the derived parts don't
> exist at those times. C++ is unlike (and superior to) Java and C# in this
> respect.

Excellent information.  I have fixed it already -- I added a "Destroy" 
function that the CChild::~CChild can call to get everything cleaned up 
nicely.

Thanks!
DanB


0
Dan
8/18/2006 8:42:49 PM
"Dan Baker" <dbmail> skrev i meddelandet 
news:eR2jebwwGHA.4200@TK2MSFTNGP04.phx.gbl...
> "Doug Harrison [MVP]" <dsh@mvps.org> wrote in message 
> news:a47ce2tot9jk90fondglsa9o1covgm5gv0@4ax.com...
>> On Fri, 18 Aug 2006 13:03:06 -0600, "Dan Baker" <dbmail> wrote:
>>
>>>I have a heavy fog over my brain, and can't see the obvious 
>>>answer...
>>>
>>>I have a base class "CBase" and an inherited class "CChild".  I 
>>>instantiate
>>>an object of "CChild", work with it, and then delete it.  It has an 
>>>empty
>>>destructor, which does nothing, and then calls the CBase 
>>>destructor, which
>>>calls a virtual function which is overridden in CChild -- BUT, it 
>>>doesn't
>>>call the overriden function (it is simply calling the base class 
>>>function).
>>>
>>>What am I not seeing here?
>>
>> In C++, virtual functions called from ctors and dtors never resolve 
>> to
>> versions overridden by derived classes, because the derived parts 
>> don't
>> exist at those times. C++ is unlike (and superior to) Java and C# 
>> in this
>> respect.
>
> Excellent information.  I have fixed it already -- I added a 
> "Destroy" function that the CChild::~CChild can call to get 
> everything cleaned up nicely.

You shouldn't really need that. Each destructor handles the 
destruction of its own part of the inheritance structure.

All of the virtual destructors are called, in reverse order (~Base 
last).


Bo Persson


0
bop (115)
8/18/2006 9:55:26 PM
> C++ is unlike (and superior to) Java and C# in this
> respect.

Curiously why would you call it superior? What exactly is the usefulness of
this?

--
Ajay Kalra [MVP - VC++]
ajaykalra@yahoo.com


0
ajaykalra (6840)
8/19/2006 1:12:55 AM
On Fri, 18 Aug 2006 21:12:55 -0400, "Ajay Kalra" <ajaykalra@yahoo.com>
wrote:

>Curiously why would you call it superior? What exactly is the usefulness of
>this?

If you call a virtual function from a base class ctor in Java or C#, the
call resolves to the most derived override, which may be in a part of the
object that hasn't yet been initialized. C++ doesn't permit this. The
downside to the C++ way is that each class in a hierarchy needs its own
vtbl, whose address has to be stuffed into the vptr as the class is created
and destroyed through application of successive ctors and dtors. Languages
like C# are simplified by using a single vtbl for the entire hierarchy.

-- 
Doug Harrison
Visual C++ MVP
0
dsh (2499)
8/19/2006 3:49:05 AM


"Doug Harrison [MVP]" <dsh@mvps.org> wrote in message
news:ehvce2926lgm339iu7r27eja0l18it44rh@4ax.com...
> On Fri, 18 Aug 2006 21:12:55 -0400, "Ajay Kalra" <ajaykalra@yahoo.com>
> wrote:
>
> >Curiously why would you call it superior? What exactly is the usefulness
of
> >this?
>

> If you call a virtual function from a base class ctor in Java or C#, the
> call resolves to the most derived override, which may be in a part of the
> object that hasn't yet been initialized.

How is that possible? Are you saying the constructor of most derived class
may not have been called?

> Languages like C# are simplified by using a single vtbl for the entire
hierarchy.

To me, this makes a lot of sense. Simple and absolutely no confusion.

--
Ajay Kalra [MVP - VC++]
ajaykalra@yahoo.com


0
ajaykalra (6840)
8/20/2006 1:27:04 AM
Reply:

Similar Artilces:

Form or Function help, please?
I've got an Excel worksheet set up where there are two columns for each day of the month. There are ten rows. A user should put an X (I'm using a colored-fill) in just ONE box in each column every day. The user should be able to change where to put the X, but shouldn't be able to enter more than one single sole solitary X in each column. I have no idea how to go about this. Right now, the sheet allows the user to enter anything they want in all ten columns. I also want to limit what the user can enter into the box they choose -- I'd prefer for them to choose a box ...

cast between base classes
Hi, Id like to check if a Control in my Dialog has a specific base class. Therefor i walk through all children by getting the CWnd object from their handle and test if the object has the base class in question, like this: Class hierarchy: ---CWnd----CButton-----CMyButton BaseClass-------------------' Doesnt work: ... CWnd * pWnd = CWnd::FromHandle(hWndChild); BaseClass pbase = dynymic_cast<BaseClass*>(pWnd); // Access Violation + CException ... Works: ... CWnd * pWnd = CWnd::FromHandle(hWndChild); if (pWnd->isKindof(RUNTIME_CLASS(CMyButton))) { ...

Weekday function
Does VBA have a function which accepts an integer input between 1 and 7, which returns a string value to represent the corresponding day of the week?? For example, if you give the function an integer value of 1, then it should give back a string of "Sunday"... and the number 7 should return "Saturday". I know I can easily write my own function, but I'm curious if VBA provides a function already. No need for me to reinvent the wheel. thank you Don't think so, you would need to write one Function MyWeekDay(DayNum As Long) As String Dim Days A...

Run function at start-up...macro?
Hi all, I can not find the best solution for the following: I have a function CheckConnection() which should check if the backend data is still there/connected. I want this function to run as very first action when the database opens. I thought to put it in the macro autoexec (feels like old DOS times) and using Openfunction, this keeps asking for a table etc. I would just like to find a method to run a user-defined function direct as Access starts. I am not familiar with macros since I never use them because I feel more covetable with VBA. Any suggestions will be appreciated a lot. ...

Fill Down based on count in another sheet
I am using the following in Excel to copy a formula: ThisWorkbook.Worksheets("Results 4").Range("3:2000").FillDown I need to only copy the formula for a certain number of rows up to 2000. The number of rows I need is based on the number of rows I have in worksheet "Results Data" in column A. So, if "Results Data" has data in column A2:A16, I will need to copy down the formula for all columns in worksheet "Results 4" to column 17. Sub FillFown() Dim lStop As Long With Worksheets("Results Data") lStop = .Cells(...

NNTP Class
Do you know where i can find an MFC class to manage newsgroup ( read header, read article )? "Bill Brother" <bigbrother@iname.com> wrote in message news:bod97a$9ci$1@reader1.imaginet.fr... > Do you know where i can find an MFC class to manage newsgroup ( read header, > read article )? > > Bill, If you do not find an MFC implementation of NNTP then do a google search for "RFC 977" (describes the NNTP protocol) and "RFC 1036" (describes the NNTP header format). Those documents will give you all of the information needed to write one yourse...

Adding wizard functions?
Hi, I created a calendar from a template but with substantial changes. How can I modify this document so that the "calendar options" function works with my new document? Keyboardhead wrote: > Hi, > I created a calendar from a template but with substantial changes. > How can I modify this document so that the "calendar options" > function works with my new document? You can't add calendar wizard functions to a custom publication. Wizards are proprietary and require you to use existing templates. -- Brian Kvalheim Microsoft Publisher MVP http://www.publi...

Conditional Formatting Based on Date
Allow me to establish the context of the situation. In column A I have a date that a form is received. In column B I want to condition the cells to display a color contingent on the amount of days that have transpired since the form was received. Column B actually documents whether a form has been approved or not. If it has not, then the appropriate color is displayed. If the form is approved, then the date of approval is put in the cell with no need for any color. The color is simply to act as an indicator of the amount of days that have passed without approval. For example, let's say ...

Convert Base 36 to base 10
I have been given a spreadsheet with transaction numbers converted into base 36- alpha numeric - I need it in base 10- number format- I have approx 30,000 of these!- is ther a formula to convert from 1 to another? -- Thanks for your help Are you sure your numbers are Base 36? I ask because I kind of suspect your "digits" are these... 0, 1, 2, ...., 9, A, B, ..., X, Y, Z and if that is the case, then you actually have Base 37 numbers and not Base 36. For Base36 numbers, the letter Z would not be in your set of digits... Z would be the 37th digit because 0 is the f...

Knowledge base features (HTML style)
Another big gap in the current CRM functionality is inability to apply HTML style formatting to the articles. I just read a post by Michael Faden and totally agree with him "I cant believe that this is not possible." KB is intended for support issues. Sometimes, a picture is worth a thousand words. In my invironment, it is almost vital to be able to apply HTML style formatting, including inserting pictures. What are the Microsoft's plans about it? Is there a way around? Thanks! ---------------- This post is a suggestion for Microsoft, and Microsoft responds to the sugges...

Which functions and Procedure to use to have desired result?
Dear Experts, Following is the my Querry, Can you guide me, please? Querry pasted, I want to attach Excel sheet, but do not konw, how to attach? Can help? I want to prepare a Summary based on the data given on two separate sheet as follow: Specification (From A on Separate Sheet) ORIGIN Material CODE Material NAME ITALY MF-001 FIOR CARNICO CHINA MF-002 HONEY ONYX ITALY MF-003 WHITE STATUARIO Bill of Quantities (From B on Separate Sheet) Location Item Description Material CODE Material NAME Rate Quantity Amount Room1 Cladding A MF-...

Default IMAP4 Virtual Server fails to start on reboot (IMAP4SVC 1036 error)
I am running Exchange 2003 on a Windows 2003 SBS. When I reboot it my server frequently fails to successfully start the Default IMAP4 Virtual Server. The Microsoft Exchange IMAP4 service starts but you cannot connect with an IMAP client. If I check ESM I find a red x on the Default IMAP4 Virtual Server. If I then manually start it usually it comes up without further trouble. Does anyone have any idea why this might be happening and more importantly how to fix it? I have to assume that it probably is due to a dependency that hasn't completed starting when the Default IMAP4 Vir...

function of "+" in the formula "=+(B19/50)*B10
I am trying to determine why the author of the spreadsheet used the plus sign in the above formula. It doesn't appear to alter the result in any way. What does it do when following the "="? Is it there to make the value positive? Or is there another function? It does nothing. In article <6C887428-AFFA-44F8-AC81-9CE400FBB221@microsoft.com>, josh66six <josh66six@discussions.microsoft.com> wrote: > I am trying to determine why the author of the spreadsheet used the plus sign > in the above formula. It doesn't appear to alter the result in any way. ...

Microsoft Knowledge Base Article
Below is a solution, however the link is dead. Anyone know what I need to add to the registry or know where I can get this file? I have this error: PUB98: Error Message: Clip Gallery Could Not Find the GIF Graphic Filter Installed CAUSE Publisher cannot insert the graphic image, because it cannot locate the correct registry entries for the GIF (Graphics Interchange Format) graphics import filter. WORKAROUND An update is available from Microsoft that adds the registry entries that are needed to use the GIF filter. To download this update, please click the following link: http://www...

Hiding Column based on If Condition
Hi, I would like to know if it is possible to hide Column/Row based on I condition. (If the cell A1 has a particular value then hide ColumnB). Incase this isn't possible, can you help me with this problem - I'm having a drop down list with Jan-Dec in Cell A1. In Cells B2:G10, need to input a value corresponding to each of the months. This valu I'm retreiving and using in a different location using If Condition The problem is that incase I change the month to Feb, the value inputted for Jan remains the same throughout. Any change made for Fe gets reflected throughout. I'm...

Events and messages and two different classes
Can I use message: WM_Timer to control and call member functions from other class ? I working in some class (CObject) which does 'hard work' e.g. calculates something and working on data, but this class hasn't message queue but I want call some member funcion periodically (period = 3sec) . Can I use message from other class e.g. dialog class and call my function periodically using WM_TIMER from that class ? To do this I must set timer from my working class and waiting to execute (expire) WM_Timer in dialog class, then information will back to my class and call my function....Is...

how to change the raw height automatically based on the content si
Sme times you need to type text in excel cell and you want the cell height to move down automatically down. Is that possible? Format > Line > auto-fit and maybe in Format > Cell choose to align with an auto wrap ...

Solution for Knowledge Base Article 289982
Does anyone know of a fix for knowledge base article 2889982 (When using Outlook and MS Word as the editor, Oulook hangs when attempting to reply or compose a message.) ...

Knowledge Base
I am having a heck of a time using this new knowledgebase. I have yet to get any relevant responses no matter how I word a search. I was able to get the old knowledgebase a few times but I can no longer find it. Any suggestions about how to use this? or better yet, how to access the "old" knowledgebase? Thanks. Are you referring to Customer Source? I also have issues with it, and always end up coming here to find my answers! "Tracey D" wrote: > I am having a heck of a time using this new knowledgebase. I have yet to get > any relevant re...

Need Verification on GP 10 function
I need verification on a function of GP 10. If GP 9.0 Standard is upgraded to GP 10.0, can the canned excel reports be refrreshed from Excel? Or does the user need to have SmartliSt Builder w/Excel Builder to use that function? Thanks. -- TC You do not need Excel Report Builder to use Excel Reports. You can make a connection to the data connections from Excel directly. -- Charles Allen, MVP "TLC" wrote: > I need verification on a function of GP 10. > > If GP 9.0 Standard is upgraded to GP 10.0, can the canned excel reports be > refrreshed from Excel?...

using the LOOKUP function
Does anyone know how to use excel to look up and return values in 3 columns to designated cells? Specifically, I have 3 columns of data...column A contains the numbers 1 through 10; column B contains certain data; column C contains more data. I need 3 drop down boxes that will allow me to choose the following: the first drop down box will allow me to choose either column B's data or column C's data. The other 2 drop down boxes will allow me to choose a number 1 through 10. In this example, if I choose column B in the first drop down box, the number 3 in the second box, and t...

Return 1 of 3 different values based on multiple cells
Need Formula or macro to allocate the "Scheduled hours" based on Group Counts: If Group "A" and "C" are both greater than 1, then Scheduled "A" and "C" should both be 6 hours If Group "A" and "C" are equal to or less than 0, then Scheduled "A" and "C" should both be 0 hours If Group "A" greater than 1, and Group "C" is equal to 0, then Scheduled "A" should 12 hours If Group "C" greater than 1, and Group "A" is equal to 0, then Schedule...

drop down values based on the another drop down
There are two drop down columns A & B, I want the drop down values of to be displayed based on the value selected for A -- Gupt ----------------------------------------------------------------------- Gupta's Profile: http://www.excelforum.com/member.php?action=getinfo&userid=2782 View this thread: http://www.excelforum.com/showthread.php?threadid=47330 Hi Gupta take a look at Debra Dalgleish's site for detailed examples of how to achieve this. http://www.contextures.com/xlDataVal01.html Regards Roger Govier Gupta wrote: >There are two drop down columns A & B...

xml schema to C-sharp classes
Hi All, I have a xml-schema and I want to generate C-Sharp classes out of it. Please let me know if there is a tool available for that. Also, I have a model(C-Sharp Classes) and I want to generate a schema out of it. Please let me know if there is a tool available for that. THnaks, Shrish Hey, There is a command line utility called XSD.EXE, which will help you to generate the classes from the .XSD file. Try out that, if need extra support post back here. -- Every thing is perfect, as long as you share!!! "shrishjain@gmail.com" wrote: > Hi All, > > I have a xml-...

email address from customer data base
Hi there! Any help would be appreciated. I am new to RMS and I have been given the job of extracting a list of email addresses from the customer data base. Does anyone know how I can do this? It seems that the crystal reports don't allow for me to filter out this one part of the customer info.. but perhaps I am not in the area, menu, or whatever. Do I need an add in for such a report? Looking forward to some advice. Thanks mlr, If you are on the RMS Annual Maintenance plan, click here and download the Customer List with Email; http://tinyurl.com/42qqo -- * "mlr alask...