Function pointer inside our own class

Related to the development libraries, released by Curt Binder
Post Reply
rimai
Posts: 12881
Joined: Fri Mar 18, 2011 6:47 pm

Function pointer inside our own class

Post 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:

Code: Select all

myarray[0];
I know I can do this outside of the class, but how would I do this from within?
Anyone have any ideas?
Roberto.
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Function pointer inside our own class

Post by lnevo »

I think you need to do this

myarray[0]();

But should be doable.
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Function pointer inside our own class

Post 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]();
rimai
Posts: 12881
Joined: Fri Mar 18, 2011 6:47 pm

Re: Function pointer inside our own class

Post 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:

Code: Select all

FuncPtr test=FeedingModeStart;
Roberto.
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Re: Function pointer inside our own class

Post by lnevo »

Maybe define the typedef as

void (ReefAngelClass::*FunctPtr)()

Check this url for another example.

http://stackoverflow.com/questions/9906 ... r-function
rimai
Posts: 12881
Joined: Fri Mar 18, 2011 6:47 pm

Re: Function pointer inside our own class

Post by rimai »

I had tried that already and gives me another error:
'FuncPtr' was not declared in this scope
Roberto.
rimai
Posts: 12881
Joined: Fri Mar 18, 2011 6:47 pm

Re: Function pointer inside our own class

Post by rimai »

Sorry...
My bad...
The error is this:
error: argument of type 'void (ReefAngelClass::)()' does not match 'void (ReefAngelClass::*)()'
Roberto.
rimai
Posts: 12881
Joined: Fri Mar 18, 2011 6:47 pm

Re: Function pointer inside our own class

Post 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'
Roberto.
rimai
Posts: 12881
Joined: Fri Mar 18, 2011 6:47 pm

Re: Function pointer inside our own class

Post 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;
Roberto.
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Function pointer inside our own class

Post by lnevo »

Try the suggestion

FuncPtr test=&ReefAngel::FeedingModeStart;
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Function pointer inside our own class

Post by lnevo »

Cool saw your post after mine...:)
Post Reply