Home > Net >  private static member in base class being acessed by derived classes
private static member in base class being acessed by derived classes

Time:10-16

I am currently using the old VC 98, and I am facing issues trying to encapsulate a static member declared as private in a base class. I wish that the derived classes do not have access to such static member, but I can't find out how. The following example code is compiling and running without problems (whereas it should not, in my opinion):

class Base
{
private:
static int integer;
};

int Base::integer=0; //initialization

class Derived : public Base
{
public:
int GetInteger(){return Base::integer;}
};

How can I make the static member inacessible in the derived class?

CodePudding user response:

How can I make the static member inaccessible in the derived class?

Use a compliant compiler. The code doesn't compile in standard C (any version) just like you expect.

  • Related