BUG: Access to Nested Base Class Member Functions Blocked

ID: Q167749


The information in this article applies to:


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:

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


  2. 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 #1

Call f() without the scope resolution operator:

class C : public A::B {
public:
  void g() { f(); }
}; 
NOTE: This will not work if there is a C::f member function defined. If that is the case, use workaround #2.

Workaround #2

Call A::B::f() via a pointer to member function:

class C : public A::B {
public:
  void g()
  {
       void (A::B::* fp)() = A::B::f;
       (this->*fp)();
  }
}; 

Additional query words:


Keywords          : kbprg kbLangCPP kbVC kbVC500bug kbVC600bug 
Version           : winnt:5.0,6.0
Platform          : winnt 
Issue type        : kbbug 

Last Reviewed: May 14, 1999