BUG: Derived Structure Can Access Private Members of Base ClassID: Q201092
|
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.
This problem is due to a bug in the compiler.
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.
}
mycode.cpp(323) : error C2248: 'Nested' : cannot access private struct declared in class 'Base' mycode.cpp(316) : see declaration of 'Nested'
Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article.
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.
}
Additional query words:
Keywords : kberrmsg kbCompiler kbVC420bug kbVC500bug kbVC600bug
Version : winnt:4.2b,5.0,6.0
Platform : winnt
Issue type : kbbug
Last Reviewed: January 21, 1999