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?
static
but is because the parameter through which the base member is being accessed is not of typeDerived
. – GemmuleBase
objects that are base class sub-objects ofDerived
objects that you can usestatic_cast
to convert fromBase*
toDerived*
in the function body. Otherwise you would have to be afriend
ofBase
or you could changevar
to be public. If you can't do any of these then you are stuck. – Gemmule