21

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?

iammilind
  • 68,093
  • 33
  • 169
  • 336
David Snape
  • 225
  • 2
  • 9
  • 2
    possible duplicate of [Accessing parent's protected variables](http://stackoverflow.com/questions/4829518/accessing-parents-protected-variables) . This is not specifically about whether the function is `static` but is because the parameter through which the base member is being accessed is not of type `Derived`. – CB Bailey Sep 05 '11 at 11:16
  • The argument to the static function must be of the base class to conform to the calling requirements of the function pointer. Maybe I could get around this using a dynamic cast – David Snape Sep 05 '11 at 11:24
  • 1
    If you can control that the function is only called with pointers to `Base` objects that are base class sub-objects of `Derived` objects that you can use `static_cast` to convert from `Base*` to `Derived*` in the function body. Otherwise you would have to be a `friend` of `Base` or you could change `var` to be public. If you can't do any of these then you are stuck. – CB Bailey Sep 05 '11 at 11:27

2 Answers2

16

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...
    }
} ;
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
James Kanze
  • 150,581
  • 18
  • 184
  • 329
  • So I guess as my static function is used for a function pointer and the argument needs to be the base class I should be able to dynamically cast to the derived class. – David Snape Sep 05 '11 at 11:27
  • 1
    If the base class has at least one virtual function, yes. (Or you can change the function to take a `Derived*`, and leave the `dynamic_cast` up to the client.) – James Kanze Sep 05 '11 at 12:04
0

Access specifier applies to the Derived class handle (reference/pointer/object) and not the methods of Derived class itself. Even if the method was not static, you would have ended up with the same error. Because you are not accessing var with the derived handle. Demo.

The correct way is to provide a setter method:

class Base {
protected:
  int var ;
public:
  void setVar(const int v) { var = v; } // <--- add this method
};

Note: There is one more way out, but I am not sure if it's elegant.

(static_cast<Derived*>(pBase))->var = 2;
iammilind
  • 68,093
  • 33
  • 169
  • 336
  • Unfortunately in my real class the data in the base class is actually some blocks of memory and a load of semaphores for multi threaded access. Using getters and setters for the semaphores and lump of memory seems very cumbersome as there are maybe 10-15 variables in the base. – David Snape Sep 05 '11 at 11:38