   Set class function as a callback
  This problem has been raised from me getting into waters on the edge of my C++ knowledge.I am currently trying to get the Newton physics SDK integrated.  So far it seems to be going ok as I am abstracting it in a way I would like for my game engine.  I have run across a problem when setting a callback function to one in another class.Here is the setup...First I have my world class bePhysicsWorld::AddEntity() and in there I am trying to set the callback function as follows.{...NewtonBodySetForceAndTorqueCallback(entity.GetBody(), bePhysicsEntity::PhysicsApplyForceAndTorque);...}My bePhysicsEntity maintains the body and any forces I added to it.Here is the class definition...class bePhysicsEntity{    private:   ...   NewtonBody *body;   vector<bePhysicsForce> forces;    public:   ...   ...   void PhysicsApplyForceAndTorque(const NewtonBody* body);};The idea is to have the callback there instead of globally so when it comes time to update the forces, they will be readily available, however, I am having trouble setting that function as the callback.  It gives me an Error in bePhysics world..."invalid use of non-static member function `void bePhysicsEntity::PhysicsApplyForceAndTorque(const NewtonBody*)'|"I have never dealt with function pointers in great deal before so I have no idea how to fix it.As a side note I tried making the PhysicsApplyForceAndTorque function static which worked but then it couldn't access the vector so I made that static as well and it worked but then it complained in my other class functions that directly accessed the vector.  Any ideas?Thanks in advance!   
  Your member function is not static. In other words, the CPU needs to know the object before it can execute the member function, the member function would then _work_ onto / within that object. However, you pass a "NewtonBody" object and a member-function-pointer of a "bePhysicsEntity". Now, how would the callback function know what object it to work on?Either make it static aka global (nothing to do with the data members of the object) or pass the (reference of the) object somehow...   ;Quote:As a side note I tried making the PhysicsApplyForceAndTorque function static which worked but then it couldn't access the vector so I made that static as well and it worked but then it complained in my other class functions that directly accessed the vector.Remember every C++ method has a "hidden" pointer to the object for which the method is called. It doesn't appear in the method signature in the way languages such as Python require you to do, but you can still access it from the inside, like this->callSomeMethodPlease().I couldn't (quick quick search) find what the callback funtion should look like, Newton's wiki only had a typedef, and didn't show what was typedef'ed. Anyway, due to your post and the error given, I believe it's something like void (*NewtonBlahBlahCallback)(void* argument).A static member function in C++, for many purposes, works just like a normal function. So, no problem in using it for a usual callback function. But you stil have the problem of the non-static member variables. Since it works like a normal function, no this pointer for you, in other words, no object, no member variables. If, in the other hand, you go and make your member variables static, no problem in accessing them inside the static method, since they won't be 'attached' to any object. Every single instance of the class you created will access the same static variable, so, make a change in one object, and everyone will get the modification. This probably isn't what you want, everyone should have it's own force vector.If you want to make your callback go to a normal method, forge a way to call the object method within the callback function. Get a pointer to it and call object->PhysicsApplyForceAndTorque(arguments)Ah, anyway, just now I remembered I could have directed you here.   ;  Here's a previous thread with a similar problem. I don't think it will help, but you never know.The way I solve these problems is usually by setting a user data pointer to the class that needs to call the function and use a function wrapper. That solution is possible in this case with the API Newton gives you.You will want to do something like this (if const is required, you will need to cast away the constness yourself and fix the parameter again):void PhysicsApplyForceAndTorqueWrapper(NewtonBody* body){   bePhysicsEntity * entity = reinterpret_cast<bePhysicsEntity *>(NewtonBodyGetUserData(body));   if(entity)   { entity->PhysicsApplyForceAndTorque(body);   }   else   { // Other code for when the body does not have an bePhysicsEntity attached   }}...bePhysicsEntity * entity = ...;NewtonBodySetUserData(entity->GetBody(), entity); NewtonBodySetForceAndTorqueCallback(entity->GetBody(), PhysicsApplyForceAndTorqueWrapper);...Now, when the body is handled in the global PhysicsApplyForceAndTorque function, you retrieve the object to your physics entity and invoke the function manually. This is basically what is done in the Win32 API along with HWNDs (SetWindowLong/GetWindowLong w/ GWL_USERDATA).   ;  Thank you all so much! This is why gamedev.net is such a great resource.  I think the user data option is a perfect way to get around this problem.  I will give these ideas a try and report back what works.Thanks again to all!   