Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I access a base classes protected members from a static function in a derived class?

Tags:

I have a program where I need to make a base class which is shared between a dll and some application code. Then I have two different derived classes, one in the dll one in the main application. Each of these have some static member functions which operate on the data in the nase class. (They need to be static as are used as function pointers elsewhere). In its simplest form my issue is shown below.

class Base {
protected:
  int var ;
};

class Derived : public Base {
  static bool Process( Base *pBase ) {
    pBase->var = 2;
    return true;
  }
};

My compiler complains that I cannot access protected members of pBase even though Derived has protected access to Base. Is there any way around this or am I misunderstanding something? I can make the Base variables public but this would be bad as in my real instance these are a lump of allocated memory and the semaphores to protect it for multithreading.

Help?

like image 784
David Snape Avatar asked Sep 05 '11 11:09

David Snape


People also ask

Can base class access protected members of derived class?

Protected members in a class are similar to private members as they cannot be accessed from outside the class. But they can be accessed by derived classes or child classes while private members cannot.

Can a derived class access static members of the base class?

You need to use the name of the base class because the variable is static and you're allowed to access it because it is protected. As explained here and here, the extra checks that don't allow access to protected members of a class from a derived class in certain circumstances don't apply to static members.

Are protected members of a base class accessed in the derived class when inherited privately in?

If a class is derived privately from a base class, all protected base class members become private members of the derived class. Class A contains one protected data member, an integer i . Because B derives from A , the members of B have access to the protected member of A .

Do derived classes inherit protected members?

protected inheritance makes the public and protected members of the base class protected in the derived class. private inheritance makes the public and protected members of the base class private in the derived class.


1 Answers

In general (regardless of whether the function is static or not), a member function of the derived class can only access protected base class members of objects of its type. It cannot access protected members of the base if the static type is not that of the derived class (or a class derived from it). So:

class Base {
protected:
    int var;
 } ;

class Derived : public Base {
    static void f1( Derived* pDerived )
    {
        pDerived->var = 2; // legal, access through Derived...
    }
    static bool Process( Base *pBase )
    {
        pBase->var = 2 ;  // illegal, access not through Derived...
    }
} ;
like image 136
James Kanze Avatar answered Sep 27 '22 03:09

James Kanze