Page 1 of 1

Function names that are pointers whaaaaa?

Posted: Wed Sep 30, 2009 5:49 pm
by JaxDragon
I was randomly looking at Lazy Foo's tuts, and I noticed his load_image function is a pointer.

Code: Select all

SDL_Surface *load_image
What I don't understand is, why would you make a function itself a pointer? Keep in mind I have a basic understanding of C++(I thought I understood pointers quite well, but apparently not).

My guess is that hes assuming you will be using that function inside another function

Code: Select all

funcFunction(&load_image) //Doesnt quite make sense
or its the equivalent of this:

Code: Select all

SDL_Surface load_image(std::string *filename )
or maybe it can be used like this

Code: Select all

void *pointerFunc(int x, int y)
to translate into this

Code: Select all

void pointerFunc(int *x, int *y)
Also, I have no idea what/how to use STL, so if it is indeed STL, let me know and I'll find out my answer when I get sams C++ in 21 days.

Re: Function names that are pointers whaaaaa?

Posted: Wed Sep 30, 2009 5:52 pm
by Joeyotrevor
Actually, this

Code: Select all

SDL_Surface *load_image()
means the function returns a pointer to a SDL_Surface. It can also be written

Code: Select all

SDL_Surface* load_image()
or

Code: Select all

SDL_Surface * load_image()
The asterisk can be anywhere between the type and the name when using pointers.


A function pointer is declared as

Code: Select all

returnType (*functionPointer)(paramType param1, paramType param2);
You can set them to point to functions like so:

Code: Select all

functionPointer = &myFunc;
and call the function they point to like this:

Code: Select all

functionPointer();
Function pointers are rarely used in OOP C++ thanks to polymorphism and virtual functions.

Re: Function names that are pointers whaaaaa?

Posted: Wed Sep 30, 2009 5:59 pm
by JaxDragon
Oh ok, now I understand.

Re: Function names that are pointers whaaaaa?

Posted: Wed Sep 30, 2009 6:32 pm
by Falco Girgis
Keep in mind that a function itself is stored somewhere in memory just as variables are. So doing this:

Code: Select all

&myFunction
is referring to the memory address of the function.

Re: Function names that are pointers whaaaaa?

Posted: Wed Sep 30, 2009 9:15 pm
by trufun202
Simply put, function pointers are badass, and damn useful.

I actually use function pointers (aka delegates in .NET) in my Snowcone Stand game. I explain this briefly in my latest video.

But, essentially I'm using function pointers to give a Button it's "OnClick" behavior without having to define it directly in the Button class. Since a button can be reused on several forms, you wouldn't want to hardcode the "OnClick" behavior. Instead, the function is defined outside of the Button class and a reference is passed in. This keeps the Button class clean and flexible.

EDIT: clean and flexible...just how I like my women.

Re: Function names that are pointers whaaaaa?

Posted: Thu Oct 01, 2009 4:16 am
by K-Bal
trufun202 wrote: But, essentially I'm using function pointers to give a Button it's "OnClick" behavior without having to define it directly in the Button class. Since a button can be reused on several forms, you wouldn't want to hardcode the "OnClick" behavior. Instead, the function is defined outside of the Button class and a reference is passed in. This keeps the Button class clean and flexible.
You should give boost::signals a try :D

Re: Function names that are pointers whaaaaa?

Posted: Thu Oct 01, 2009 9:07 am
by trufun202
K-Bal wrote:You should give boost::signals a try :D
Sweet, that will handy when I get to menus and UI for Golvellius, which is built in C++.

.NET actually has an event system built into it. Broadcasting an "OnClick" event is really more customary, but since all of my controls are sharing the same interface, I found it to be cleaner to use delegates.

Re: Function names that are pointers whaaaaa?

Posted: Wed Oct 07, 2009 10:00 am
by avansc
has anyone mentioned threading? dont think you can do those without function pointers.

Re: Function names that are pointers whaaaaa?

Posted: Wed Oct 07, 2009 10:06 am
by trufun202
avansc wrote:has anyone mentioned threading? dont think you can do those without function pointers.
yep, that's true. Same goes for asynchronous callbacks.