BUG: Wrong Pointer Value When Nested Classes Have Same Name

Last reviewed: July 24, 1997
Article ID: Q143082
The information in this article applies to:
  • The Microsoft C/C++ Compiler (CL.EXE) included with: - Microsoft Visual C++, 32-bit Edition, versions 2.0, 2.1, 2.2, 4.0,

         4.1, 4.2, 5.0
    

SYMPTOMS

When a class is derived from multiple classes such that two or more of the base classes are nested classes of the same name, pointers to either of the nested base classes point to the same address. Consider a class D which is derived from both B1::Nested and B2::Nested. Given an object d, which is of type D, then (B1::Nested *)&d and (B2::Nested *)&d will both resolve to the same address. If either of the nested base class' names are changed to be unique, the behavior is normal.

RESOLUTION

This happens only when the nested classes have the same name. Change the names, for example "A::NestedA" and "B::NestedB".

STATUS

Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. We are researching this problem and will post new information here in the Microsoft Knowledge Base as it becomes available.

MORE INFORMATION

Sample Code to Reproduces Error

   // Compile option needed: none

   // File test.cpp

   struct A {
       struct Nested {
           virtual void A_Func() = 0;
       };
   };

   struct B {
       struct Nested {
           virtual void B_Func() = 0;
       };
   };

   struct MyClass : public A::Nested, public B::Nested
   {
       void A_Func() { cout << "A_Func() called" << endl; }
       void B_Func() { cout << "B_Func() called" << endl; }
   };

   void main()
   {

       MyClass m;
       cout << "(A::Nested*)&m = " << (void*)(A::Nested*)&m << endl;
       cout << "(B::Nested*)&m = " << (void*)(B::Nested*)&m << endl;
       ((A::Nested*)&m)->A_Func();
       ((B::Nested*)&m)->B_Func();
   }

In this example, the compiler confuses (A::Nested*) and (B::Nested*), so the same values are printed for both ((A::Nested*)&m) and ((B::Nested*)&m) when they should be different. This also causes the call to B_Func on the last line to call A_Func instead.


Keywords : CPPLangIss vcbuglist400 vcbuglist500 kbbuglist kbprg
Version : 4.0 4.1 4.2 2.0 2.1 2.2 5.0
Platform : NT WINDOWS
Issue type : kbbug


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: July 24, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.