Member function pointer
-
Hello , ive got the following member function pointer problem noting that all my search result on the net wasn't useful at all. class MyClass { Anotherclass MyObject; void Function1(); void FunctionNeededPointer(Float*,int,int); }; void MyClass::Function1() { //Here i need to pass a pointer of a function to the //following function belonging to MyObject //void Anotherclass::Function(void *aFunction,void // *callingObject=NULL) //so i tried this and didn't work void (MyClass::*pToFunction)(float*,int,int) = &MyClass::FunctionNeededPointer; MyObject.Function((void*)pToFunction); //oops error: cannot convert from void(__thiscall // Myclass::*)(float*,int,int) to void* //So had to make that function static and worked but //it ended up that every thing in MyClass was converted //to static which is not a nice solution at all //so i'm wondering if someone could help with this //problem }
-
Hello , ive got the following member function pointer problem noting that all my search result on the net wasn't useful at all. class MyClass { Anotherclass MyObject; void Function1(); void FunctionNeededPointer(Float*,int,int); }; void MyClass::Function1() { //Here i need to pass a pointer of a function to the //following function belonging to MyObject //void Anotherclass::Function(void *aFunction,void // *callingObject=NULL) //so i tried this and didn't work void (MyClass::*pToFunction)(float*,int,int) = &MyClass::FunctionNeededPointer; MyObject.Function((void*)pToFunction); //oops error: cannot convert from void(__thiscall // Myclass::*)(float*,int,int) to void* //So had to make that function static and worked but //it ended up that every thing in MyClass was converted //to static which is not a nice solution at all //so i'm wondering if someone could help with this //problem }
With classes you have two options: 1) make function static, so that all objects of the type MyClass reference the same static function. In this case you can use MyClass::FunctionNeededPointer 2) Function you are trying to pass is not static. To pass function pointer you should use something like "this->FunctionNeededPointer", since function obviously has very little meaning outside of class scope... 3) Here is how you can define function type typedef void (MYFUNCTION)(float*,int,int); void Function(MYFUNCTION *p); { Function(&this->FunctionNeededPointer); } Brian
-
With classes you have two options: 1) make function static, so that all objects of the type MyClass reference the same static function. In this case you can use MyClass::FunctionNeededPointer 2) Function you are trying to pass is not static. To pass function pointer you should use something like "this->FunctionNeededPointer", since function obviously has very little meaning outside of class scope... 3) Here is how you can define function type typedef void (MYFUNCTION)(float*,int,int); void Function(MYFUNCTION *p); { Function(&this->FunctionNeededPointer); } Brian
Thanks for your reply, but i forgot to mention something that i haven't got any control on the function Function(void *aFunction , void *callingObject=null) as it came with a header file of a library and i've got to use it as is, besides when i try to use &this->FuctionNeededPointer it gives the following error '&' : illegal operation on bound member function expression