gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > Virtual functions.

#16882 - Krakken - Thu Feb 26, 2004 9:05 am

Hi,

Can anyone please give me a quick example on how to store a pointer to a function in a variable and then call that function from that variable - I seem to have forgotten.

This is what I mean:
Code:

void function_to_call (bool x) { // BODY // }

??? call_by_this = function_to_call;
call_by_this(true);

#16884 - tepples - Thu Feb 26, 2004 9:20 am

http://www.function-pointer.org/
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#16890 - poslundc - Thu Feb 26, 2004 2:31 pm

tepples wrote:
http://www.function-pointer.org/


Man, the Internet is a strange place...

Dan.

#16918 - sajiimori - Fri Feb 27, 2004 12:23 am

Quoth C-IAQ:

1.11: How do I declare an array of N pointers to functions returning pointers to functions returning pointers to characters?

Well, first you need to know how to declare an array of N items of type T - that's
Code:

T foo[N];

Now you need to look at how to declare a pointer to function returning something, say, an object of type S. That's like this:
Code:

S (*bar)();

Now assume that S is ``pointer to function returning pointer to char''. We get
Code:

(char *) (*)() (*bar)();

So, the whole thing turns out to be (with appropriate parentheses)
Code:

(((char)(*))((*)())(((*)((foo)))())([(N)]));

If your compiler complains, break this down into subexpressions.

To call it, just use
Code:

foo[i]();

This works because, in C, declaration reflects use, but it's one of those weird distorted mirrors.

#17028 - Miked0801 - Sun Feb 29, 2004 7:28 am

Argh, my brain!

Seriously, I got the syntax of a funcion once and made a typecast of it and forgot about it :)

#17096 - Krakken - Mon Mar 01, 2004 1:00 am

I know what you mean. Thanks all!

#17366 - LOst? - Sat Mar 06, 2004 11:41 pm

poslundc wrote:
tepples wrote:
http://www.function-pointer.org/


Man, the Internet is a strange place...

Dan.


Wow, cool place! Finally I cqan learn about using pointers to C++ members