Skip to main content
gamedev.net
🔒 Locked

Pointers

Started by anupgupta 0 replies 1k views
Original Post
anupgupta
anupgupta
hello

I have a query as to how to pass a member function from a class via function pointer..
the example is as follows

class CSound
{
public:
void playSound(int index);
void stopSound(int index);

//Memory Allocation Functions
void MemAlloc(int bytes, int use, int actUse);
void MemFree(void *mem);

};

now i call a function which is the part of the API and pass the functions in the CSound class via function pointer to the function...
sndRegisterMainMemAllocator(MemAlloc, MemFree);

but here the compiler always gives me an error..
error 1297: argument of type "void (*) (int , int, int*)" is incompatible with parameter (1 of sndRegisterMainMemAllocator) of type "ExternSndMemAlloc"

...In C i know just passing the name of the function works...but in c++ i am a little confused whether just passing the name of the member function of the class will work...

i also tried making the member functions as static but then too i get the same error...
Please advice on this....as how to get it working...any suggestions would be most helpful...
Hodgman
Hodgman
Quote:
Original post by anupgupta
now i call a function which is the part of the API and pass the functions in the CSound class via function pointer to the function...
sndRegisterMainMemAllocator(MemAlloc, MemFree);
If this API expects a function pointer, then you won't be able to give it a pointer-to-member-function because they are very different. The main difference is that you need a this pointer in order to call a member-function.
Quote:
but here the compiler always gives me an error..
error 1297: argument of type "void (*) (int , int, int*)" is incompatible with parameter (1 of sndRegisterMainMemAllocator) of type "ExternSndMemAlloc"
The function you posted has the signature "void (int, int, int)" and that error is complaining about "void (int, int, int*)" - these two signatures are different.
What exactly is MemAlloc typedefed/defined as?
Quote:
i also tried making the member functions as static but then too i get the same error...
A static member function will work, as it doesn't require a this pointer. You're getting the error for the reason above.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.