FIX: C++ 7.0 Doesn't Allow Explicit Virtual Destructor Call
PSS ID Number: Q104040
Article last modified on 07-14-1994

7.00

MS-DOS


----------------------------------------------------------------------
The information in this article applies to:

 - Microsoft C/C++ for MS-DOS, version 7.0
----------------------------------------------------------------------

SYMPTOMS
========

In Microsoft C++ 7.0, the compiler gives many syntax errors when
trying to explicitly call a destructor in a manner that preserves the
virtual function call mechanism as shown in section 12.4 of "The
Annotated C++ Reference Manual." The sample code below  generates the
following errors when the problem lines are uncommented:

   test.cpp(30) : error C2059: syntax error : '~'
   test.cpp(32) : error C2039: 'i' : is not a member of 'B'
   test.cpp(32) : error C2039: 'i' : is not a member of 'B'
   test.cpp(32) : error C2039: 'i' : is not a member of 'B'
   test.cpp(33) : error C2039: 'arr' : is not a member of 'B'
   test.cpp(33) : error C2039: 'i' : is not a member of 'B'

RESOLUTION
==========

Provide a virtual member function that explicitly calls the destructor
on itself. The sample code demonstrates the workaround as well as the
error.

STATUS
======

Microsoft has confirmed this to be a problem in Microsoft C/C++
version 7.0. The problem was corrected in Microsoft C/C++ version 8.0,
included with Visual C++ for Windows, version 1.0.

Sample Code
-----------

// command line options: none

#include<iostream.h>

class B
{
public:
   B( ){  cout << "I'm in the ctor of B"<<endl;}
   virtual ~B(){cout << "I'm in the dtor of B"<<endl;}
   void virtual destroy(){ this->B::~B();};
};

class D:public B
{
public:
   D(){cout << "I'm in the ctor of D"<<endl;}
   ~D(){cout << "I'm in the dtor of D"<<endl;}
   void destroy(){ this->D::~D();};
};

void main()
{
   B *arr[5];

   for(int i=0;i<5;i++)
   {
      arr[i]=(i%2) ? new B: new D;
   }

 //for( i=0; i< 5; i++)   /* Uncomment these two lines to generate  */
 //   arr[i]->~B();       /* the syntax errors.                     */

   for( i=0; i<5;i++)     /* Use this instead with C++ version 7.0  */
      arr[i]->destroy();  /* to get around the syntax errors.       */
}

Additional reference words: 7.0
KBCategory: Tls
KBSubCategory: CPPIss

=============================================================================

Copyright Microsoft Corporation 1994.
