Set Callback from class

Started by
11 comments, last by VanillaSnake21 17 years, 1 month ago
Hi, I'm using Newton Pshyics SDK and it requires you to set a callback for one pf it's functions, I'm trying to wrap everything in my own class, but when I try to set the callback from within the class I am getting a compiler error that says that the parameter of a callback cannot be converted to a certain function. But when I use a function that is not in a class to set the callback, it works fine, how can I fix this problem. Thanks.

You didn't come into this world. You came out of it, like a wave from the ocean. You are not a stranger here. -Alan Watts

Advertisement
Quote:Original post by VanillaSnake21
Hi, I'm using Newton Pshyics SDK and it requires you to set a callback for one pf it's functions, I'm trying to wrap everything in my own class, but when I try to set the callback from within the class I am getting a compiler error that says that the parameter of a callback cannot be converted to a certain function. But when I use a function that is not in a class to set the callback, it works fine, how can I fix this problem. Thanks.
It sounds like you're trying to submit a class member function as the callback, which almost certainly won't work (I'm making some assumptions here about the Newton API).

Just so we have a little more to go on, perhaps you could post a bit of code, e.g. the class in question, the code where you set the callback, and perhaps the declaration of the corresponding Newton API function (or maybe a link to where we can read about it). Information about the purpose of the callback would be useful as well.

Given some more information, I'm sure the gurus here will be able to offer some good suggestions as to how you might work around the member function pointer problem.
Class member functions are not the same as "plain" functions; they employ a different calling convention - __thiscall vs __stdcall - to allow for the implicit passing of the pointer to the current object. Static member functions, however, use the __stdcall convention because they belong to the class and do not have access to instance members, so you can pass a static member function to the physics SDK.

If you need access to a class object instance, you will need some mechanism for registering an association between a pointer to an object instance and an index available to the physics SDK, which it passes to the static member function. The static function uses that index to retrieve the object instance pointer. The canonical example is abstraction of Win32 HWND into classes.
[source lang = cpp]void PhysicsObj::PhysicsApplyForceAndTorque(const NewtonBody* Body, float Gravity){	dFloat Ixx;	dFloat Iyy;	dFloat Izz;	dFloat Mass;	NewtonBodyGetMassMatrix(Body, &Mass, &Ixx, &Iyy, &Izz);	D3DXVECTOR3 force(0.0f, Mass* Gravity, 0.0f);	NewtonBodySetForce(Body, force);}void PhysicsObj::SetGravityCallback(){			NewtonBody* Body;	NewtonBodySetForceAndTorqueCallback(Body, PhysicsApplyForceAndTorque);}these are my funtions which are within a single class, and they are both static, but I'm still getting the same error.


Edit: these are the decls in .h file

		static void PhysicsApplyForceAndTorque(const NewtonBody* Body, float Gravity);		static void SetGravityCallback();

You didn't come into this world. You came out of it, like a wave from the ocean. You are not a stranger here. -Alan Watts

Quote:Original post by Oluseyi
Class member functions are not the same as "plain" functions; they employ a different calling convention - __thiscall vs __stdcall - to allow for the implicit passing of the pointer to the current object. Static member functions, however, use the __stdcall convention because they belong to the class and do not have access to instance members, so you can pass a static member function to the physics SDK.
It's more than that though. Non-static class member function pointers typically are larger than function pointers. The size of the non-static class member function pointer depends heavily on the class, if the function is virtual if the class inherits from another class, if the parent class has a virtual function of the same signature, if virtual inheritance is used in the inheritance tree, etc.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

Quote:Original post by Washu
Quote:Original post by Oluseyi
Class member functions are not the same as "plain" functions; they employ a different calling convention - __thiscall vs __stdcall - to allow for the implicit passing of the pointer to the current object. Static member functions, however, use the __stdcall convention because they belong to the class and do not have access to instance members, so you can pass a static member function to the physics SDK.
It's more than that though. Non-static class member function pointers typically are larger than function pointers. The size of the non-static class member function pointer depends heavily on the class, if the function is virtual if the class inherits from another class, if the parent class has a virtual function of the same signature, if virtual inheritance is used in the inheritance tree, etc.


But I'm not inheriting my class, and both methods are static.

You didn't come into this world. You came out of it, like a wave from the ocean. You are not a stranger here. -Alan Watts

Quote:Original post by VanillaSnake21
But I'm not inheriting my class, and both methods are static.


Show the code and the exact error, please. We're not psychic.
I'm smelling the perfect place to use Boost to send Newton that class function.
Quote:Original post by Zahlman
Quote:Original post by VanillaSnake21
But I'm not inheriting my class, and both methods are static.


Show the code and the exact error, please. We're not psychic.


The code is already shown, unless you want me to post up the entire class header ,and implementation, which i think would be pretty useless since those two lines is where all the action is taking place, and the error is


c:\Documents and Settings\Owner\My Documents\Visual Studio Projects\Physics Tester 2003\Physics.cpp(34) :
error C2664: 'NewtonBodySetForceAndTorqueCallback' : cannot convert parameter 2 from 'void (const NewtonBody *,float)' to 'NewtonApplyForceAndTorque'
None of the functions with this name in scope match the target type
Quote:Original post by Zahlman
Quote:Original post by VanillaSnake21
But I'm not inheriting my class, and both methods are static.


Show the code and the exact error, please. We're not psychic.


The code is already shown, unless you want me to post up the entire class header ,and implementation, which i think would be pretty useless since those two lines is where all the action is taking place, and the error is


c:\Documents and Settings\Owner\My Documents\Visual Studio Projects\Physics Tester 2003\Physics.cpp(34) :
error C2664: 'NewtonBodySetForceAndTorqueCallback' : cannot convert parameter 2 from 'void (const NewtonBody *,float)' to 'NewtonApplyForceAndTorque'
None of the functions with this name in scope match the target type

Sorry for two post, pressed post twice

You didn't come into this world. You came out of it, like a wave from the ocean. You are not a stranger here. -Alan Watts

This topic is closed to new replies.

Advertisement