So all I need to do is set that function to static. Thanks.
However, I found that I also have to set every variables used in that function to static. That leads to making class B static which leads to unresolved external symbols from static B b[40][40] in another class. I'll try another approach.
No - you can access protected and private class members in a static member function. Consider:
class A {
protected:
int protectedBaseMember;
};
class B : public A {
private:
int privateMember;
static void StaticFunc(A* obj);
};
//static
void B::StaticFunc(A* obj)
{
B* b = (B*)obj;
//this is perfectly legal and accesses two non-static members of B
b->privateMember = b->protectedBaseMember;
}
A static member function can access any member of its class just like a normal member function, but has to do so through a named object rather than the this pointer. All you need to do is make sure the object is properly initialized and arrives in the static function through some means (doesn't need to be passed in as an argument).