I recently stumbled across a bug in the Microsoft Visual C++ 6.0 compiler. There doesn't seem to be a Knowledge Base article on this particular bug (Knowledge Base search results for C2248 error) but I did discover a workaround so thought I'd share it here in the hope that it may be helpful to others.

Symptoms

In the sample code in this article, class C1 contains a private class definition for a class inner. C1::inner has a method called self that returns a reference to C1::inner and makes an assignment to a local variable within the method body. Compiling the sample code results in the following compiler error:

error C2248: 'inner' : cannot access private class declared in class "C1"

Resolution

Perform some operation (even void()) before making the assignment within the self function.

Status

Microsoft appears to be unaware of this bug. Knowledge Base search results for C2248 error.

More information

For this compiler bug to occur, C1::inner must be declared private and C1::inner::self must return C1::inner or a reference to it. An assignment at the beginning of any code block within C1::inner::self will cause the bug, i.e. not just the function-level block but also any if, while, for blocks.

Sample Code

class C1
{
private:
    class inner
    {
    public:
        inner& self();
    };

};
 
C1::inner& C1::inner::self()
{
    #ifdef WORKAROUND
        void();
    #endif
 
    int i=0; // this line generates the C2248 error
    return *this;
}

Applies to

  • Microsoft Visual C++ 6.0 Professional Edition, Service Pack 6
  • Possibly others - please leave a comment to let me know

Keywords: kbbug kbcompiler kbcpponly