BUG: Access to Nested Base Class Member Functions Blocked
ID: Q167749
|
The information in this article applies to:
-
Microsoft Visual C++, 32-bit Editions, versions 5.0, 6.0
SYMPTOMS
When a class derived from a nested class calls a base class member function
explicitly, the compiler generates error C2352: (relative to the sample
code below):
main.cpp(13) : error C2352: 'A::B::f' : illegal call of non-static
member function
RESOLUTION
There are 2 workarounds:
- Call the member function without the scope resolution operator.
This is the simplest workaround, but it will not work if a member
function of the same name is defined in the derived class. Please see
workaround #1 after the sample code below.
- Declare a pointer to member function and call the base class function
via the pointer. Please see workaround #2 after the sample code
below.
STATUS
Microsoft has confirmed this to be a bug in the Microsoft products listed
at the beginning of this article.
MORE INFORMATION
Sample Code
/* Compile Options: None */
class A {
public:
class B {
public:
void f();
};
};
class C : public A::B {
public:
void g() { A::B::f(); } // <== C2352 at this line
};
Workaround
Use a typedef for class A::B as follows:
class C : public A::B
{
public:
typedef A::B BaseClass;
void f() { BaseClass::f(); } // <== No Error Any More
};
====================
BEGIN JUSTIFICATION:
====================
<B>Workaround #1</B>
Will not work if there is a C::f member function defined, including the
common case of overloading the base class function f().
<B>Workaround #2</B>
Causes an infinite recursion for virtual functions g() that needs to
call the base class virtual function g().
==================
END JUSTIFICATION:
==================
Additional query words:
Keywords : kbprg kbLangCPP kbVC kbVC500bug kbVC600bug
Version : winnt:5.0,6.0
Platform : winnt
Issue type : kbbug