Article ID: 143082 - Last Review: July 5, 2005 - Revision: 1.1

FIX: Wrong Pointer Value When Nested Classes Have Same Name

This article was previously published under Q143082

On This Page

Expand all | Collapse all

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

This bug was corrected in Microsoft Visual C++, version 6.0.

MORE INFORMATION

Sample Code to Reproduce Error

// Compile option needed: none

// File test.cpp

truct A {

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

};

truct B {

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

};

truct 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.

APPLIES TO
  • Microsoft Visual C++ 2.0 Professional Edition
  • Microsoft Visual C++ 2.1
  • Microsoft Visual C++ 2.2
  • Microsoft Visual C++ 4.0 Standard Edition
  • Microsoft Visual C++ 4.1 Subscription
  • Microsoft Visual C++ 4.2 Enterprise Edition
  • Microsoft Visual C++ 4.2 Professional Edition
  • Microsoft Visual C++ 5.0 Enterprise Edition
  • Microsoft Visual C++ 5.0 Professional Edition
Keywords: 
kbbug kbfix kbvc600fix KB143082
Retired KB ArticleRetired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.
 

Article Translations