Microsoft KB Archive/201092

= FIX: Derived Structure Can Access Private Members of Base Class =

Article ID: 201092

Article Last Modified on 12/11/2003

-

APPLIES TO


 * Microsoft Visual C++ 4.2 Enterprise Edition
 * Microsoft Visual C++ 5.0 Enterprise Edition
 * Microsoft Visual C++ 6.0 Enterprise Edition
 * Microsoft Visual C++ 4.2 Professional Edition
 * Microsoft Visual C++ 5.0 Professional Edition
 * Microsoft Visual C++ 6.0 Professional Edition
 * Microsoft Visual C++ 6.0 Standard Edition

-



This article was previously published under Q201092



SYMPTOMS
No compiler error or warning is generated when a struct derives publicly from a class and accesses a private nested structure belonging to the base class.



CAUSE
This problem is due to a bug in the compiler.



RESOLUTION
When accessing the nested structures of the base class in the derived class, use the full qualified name to get the compiler to generate an error if the access to the nested members of the base class is illegal.

For example, the following code uses the full qualified name:

class Base {   struct Nested {       int i;    }; };

struct Derived : public Base {   Base::Nested m_Nested; };

void main(void) {   Derived test; test.m_Nested.i = 100; //Generates compile-time error. }

The error message displayed is:

mycode.cpp(323) : error C2248: 'Nested' : cannot access private struct declared in class 'Base' mycode.cpp(316) : see declaration of 'Nested'



STATUS
Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article.

This problem was corrected in Microsoft Visual C++ .NET.



Steps to Reproduce Behavior

 * 1) Create a new console application.
 * 2) Add a new C++ source file into the project.
 * 3) Add the code below:

class Base {   struct Nested {       int i;    }; };

struct Derived : public Base {   Nested m_Nested; };

void main(void) {   Derived test; test.m_Nested.i = 100; //No compile-time error is generated. }

Keywords: kberrmsg kbbug kbfix kbnoupdate kbcompiler KB201092

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.