Page 1 of 1
Function pointer inside our own class
Posted: Sun Apr 07, 2013 12:13 pm
by rimai
Hey Everyone,
I'm getting confused and can't get this to work.
Is it possible to have a pointer to a function inside our own class?
The class in reference is the ReefAngelClass and function is FeedingModeStart();
Example:
Code: Select all
typedef void (* FuncPtr) (); // declare function pointers
FuncPtr test=FeedingModeStart;
Maybe I'm going the wrong path....
So, let me give you an overview of what I'd like to do.
I'd like to have an array of function pointers that I can call by using the array reference.
Something like:
Code: Select all
FuncPtr myarray={FeedingModeStart,WaterChangeModeStart};
This way I can call, for example:
I know I can do this outside of the class, but how would I do this from within?
Anyone have any ideas?
Function pointer inside our own class
Posted: Sun Apr 07, 2013 12:19 pm
by lnevo
I think you need to do this
myarray[0]();
But should be doable.
Function pointer inside our own class
Posted: Sun Apr 07, 2013 12:20 pm
by lnevo
I don't think it would be any different than calling another function inside the class.
Outside the class it could still be called if it were public
ReefAngel.myarray[0]();
Re: Function pointer inside our own class
Posted: Sun Apr 07, 2013 12:23 pm
by rimai
The problem is that I can't even get past the compiler

The compiler doesn't like the function to be inside of the class.
I get this error:
error: argument of type 'void (ReefAngelClass::)()' does not match 'void (*)()'
on this line:
Re: Function pointer inside our own class
Posted: Sun Apr 07, 2013 12:34 pm
by lnevo
Maybe define the typedef as
void (ReefAngelClass::*FunctPtr)()
Check this url for another example.
http://stackoverflow.com/questions/9906 ... r-function
Re: Function pointer inside our own class
Posted: Sun Apr 07, 2013 12:46 pm
by rimai
I had tried that already and gives me another error:
'FuncPtr' was not declared in this scope
Re: Function pointer inside our own class
Posted: Sun Apr 07, 2013 12:53 pm
by rimai
Sorry...
My bad...
The error is this:
error: argument of type 'void (ReefAngelClass::)()' does not match 'void (ReefAngelClass::*)()'
Re: Function pointer inside our own class
Posted: Sun Apr 07, 2013 12:56 pm
by rimai
And if I do this:
Code: Select all
typedef void (ReefAngelClass::* FuncPtr) (); // declare function pointers
FuncPtr test=&FeedingModeStart;
I get this:
error: ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function. Say '&ReefAngelClass::FeedingModeStart'
Re: Function pointer inside our own class
Posted: Sun Apr 07, 2013 1:01 pm
by rimai
I think I got it...
Thanks for that link. It led me to another one that pointed me to add the class in front of the function too.
Let me test it out
Code: Select all
typedef void (ReefAngelClass::* FuncPtr) (); // declare function pointers
FuncPtr test=&ReefAngelClass::FeedingModeStart;
Function pointer inside our own class
Posted: Sun Apr 07, 2013 1:03 pm
by lnevo
Try the suggestion
FuncPtr test=&ReefAngel::FeedingModeStart;
Function pointer inside our own class
Posted: Sun Apr 07, 2013 1:04 pm
by lnevo
Cool saw your post after mine...
