FIX: Call to Member Function with Virtual Base Fails

Last reviewed: September 18, 1997
Article ID: Q115849
1.00 WINDOWS kbtool kbfixlist kbbuglist

The information in this article applies to:

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

        - Microsoft Visual C++ for Windows, version 1.0
    

SYMPTOMS

The use of the /Ob2, /Oe, and /Og optimization switches may cause calls to a member function of a class with a virtual base class to return incorrect values.

RESOLUTION

There are several workarounds to this problem:

  1. Use the /Ob1 optimization instead of /Ob2

    -or-

  2. Use the fast compiler option /f.

    -or-

  3. Disable optimization by either removing the /Oe and /Og switches.

    -or-

  4. Disable optimization locally using the optimize pragma:

          #pragma optimize("",off)
    

          void classname::classfunction()
          {
          }
    

          #pragma optimize("",on)
    

STATUS

Microsoft has confirmed this to be a problem in the Microsoft products listed above. This is not a problem in Visual C++, 32-bit Edition. This problem was corrected in Visual C++ version 1.5.

MORE INFORMATION

The sample code below can be used to illustrate this problem.

Sample Code

/* Compile options needed: /Ob2 /AC (or /AL or /AH)
*/

#include <stdio.h>
#include <stdlib.h>

class A {
 public:
   int a;
   int b;
   int z;
   int funcA(int);
};

int A::funcA(int i)
{
   return a + b + i;
}

class B : virtual public A {

 public:
   int c;
   int d;
   int z;
   int funcA(int);
   int funcB(int);
};

int B::funcA(int i)
{
   return a + c + i;
}

int B::funcB(int i)
{
   return c + d + i;
}

void main(void)
{
   A a; a.a = 1; a.b = 2;
   B b; b.a = 1; b.b = 2; b.c = 3; b.d = 4;

   if (a.funcA(1) != 4) printf("FAILED on line %d\n", __LINE__);

   \\ Error occurs on this line
   if (b.funcA(1) != 5) printf("FAILED on line %d\n", __LINE__);

   if (b.funcB(1) != 8) printf("FAILED on line %d\n", __LINE__);

   return 0;
}


Additional reference words: 1.00 8.00
KBCategory: kbtool kbfixlist kbbuglist
KBSubcategory: CPPIss
Keywords : CPPIss kb16bitonly kbbuglist kbfixlist kbtool
Version : 1.00
Platform : 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.