FIX: Call to Member Function with Virtual Base Fails

ID: Q115849

1.00 WINDOWS kbtool kbfixlist kbbuglist

The information in this article applies to:

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          : kb16bitonly kbCompiler kbCPPonly kbVC kbbuglist kbfixlist
Version           : 1.00
Platform          : WINDOWS
Solution Type     : kbfix

Last Reviewed: September 22, 1997