FIX: C2662: "'__vbaseDtor' cannot convert 'this' pointer"

Last reviewed: September 18, 1997
Article ID: Q117833
7.00 | 1.00 1.50 | 1.00
MS-DOS | WINDOWS   | WINDOWS NT
kbtool kbfixlist kbbuglist

The information in this article applies to:

  • The Microsoft C/C++ Compiler (CL.EXE), included with:

        - Microsoft C/C++ for MS-DOS, version 7.0
        - Microsoft Visual C++ for Windows, versions 1.0 and 1.5
        - Microsoft Visual C++ 32-bit Edition, version 1.0
    

SYMPTOMS

Compiling the sample code shown below using the C/C++ compilers for MS-DOS versions 7.0, 8.0, and 8.0c, or the C/C++ 32-bit compiler for Windows NT, version 8.0, causes the compiler to generate one of the following error messages:

   error C2662: '__vbaseDtor': cannot convert 'this'
   pointer from 'const class::DerivedClass __near *'
   to 'class::DerivedClass __near *const'

   -or-

   error C2662: '__vbaseDtor' : cannot convert 'this'
   pointer from 'const class ::DerivedClass *'
   to 'class ::DerivedClass *const '

The error message usually is generated when the program tries to instantiate a local constant object of a class that is virtually derived from a base class in which a user-defined destructor is defined.

RESOLUTION

To alleviate the C2662 error, use one of the following workarounds:

  • Instantiate the constant object in file scope. For example, declare it as a global object.

    -or-

  • Delare the constant object static. For example:

          static const DerivedClass myobject;
    

    -or-

  • Use the compiler supplied default destructor for the base class by removing the user-defined destructor in the base class.

    -or-

  • Do not declare the object constant by removing keyword const.

    -or-

  • If possible, do not derive the class virtually.

STATUS

Microsoft has confirmed this to be a problem in the products and versions listed above. This problem was corrected in C/C++ compiler version 9.0, included with Visual C++ 32-bit Edition, version 2.0.

MORE INFORMATION

The following sample code can be used to demonstrate the problem:

Sample Code

/* Compile options needed: None
*/

   class BaseClass
   {
      public:
        ~BaseClass(){}
   };

   class DerivedClass: virtual public BaseClass
   {
     public:
       DerivedClass(){}
       ~DerivedClass(){}
   };

   void main()
   {
     const DerivedClass _myClass;
   }


Additional reference words: 1.00 1.50 7.00 8.00 8.00c
KBCategory: kbtool kbfixlist kbbuglist
KBSubcategory: CPPIss
Keywords : CPPIss kbbuglist kbfixlist kbtool
Version : 7.00 | 1.00 1.50 | 1.00
Platform : MS-DOS NT WINDOWS
Solution Type : kbfix


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: September 18, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.