Function names that are pointers whaaaaa?

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
JaxDragon
Chaos Rift Junior
Chaos Rift Junior
Posts: 395
Joined: Mon Aug 04, 2008 2:03 pm
Current Project: Kanoba Engine
Favorite Gaming Platforms: PS3, PC
Programming Language of Choice: C++
Location: Northeast NC

Function names that are pointers whaaaaa?

Post 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.
User avatar
Joeyotrevor
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 62
Joined: Thu Jan 22, 2009 6:24 pm
Programming Language of Choice: C++

Re: Function names that are pointers whaaaaa?

Post 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.
Last edited by Joeyotrevor on Wed Sep 30, 2009 6:00 pm, edited 1 time in total.

Code: Select all

eb 0c 48 65 6c 6c 6f 20 77 6f 72 6c 64 21 31 d2 8e c2 30 ff b3 0a bd 02 7c b9 0b 00 b8 00 13 cd 10 eb fe
User avatar
JaxDragon
Chaos Rift Junior
Chaos Rift Junior
Posts: 395
Joined: Mon Aug 04, 2008 2:03 pm
Current Project: Kanoba Engine
Favorite Gaming Platforms: PS3, PC
Programming Language of Choice: C++
Location: Northeast NC

Re: Function names that are pointers whaaaaa?

Post by JaxDragon »

Oh ok, now I understand.
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Function names that are pointers whaaaaa?

Post 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.
User avatar
trufun202
Game Developer
Game Developer
Posts: 1105
Joined: Sun Sep 21, 2008 12:27 am
Location: Dallas, TX
Contact:

Re: Function names that are pointers whaaaaa?

Post 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.
-Chris

YouTube | Twitter | Rad Raygun

“REAL ARTISTS SHIP” - Steve Jobs
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

Re: Function names that are pointers whaaaaa?

Post 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
User avatar
trufun202
Game Developer
Game Developer
Posts: 1105
Joined: Sun Sep 21, 2008 12:27 am
Location: Dallas, TX
Contact:

Re: Function names that are pointers whaaaaa?

Post 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.
-Chris

YouTube | Twitter | Rad Raygun

“REAL ARTISTS SHIP” - Steve Jobs
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Function names that are pointers whaaaaa?

Post by avansc »

has anyone mentioned threading? dont think you can do those without function pointers.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
trufun202
Game Developer
Game Developer
Posts: 1105
Joined: Sun Sep 21, 2008 12:27 am
Location: Dallas, TX
Contact:

Re: Function names that are pointers whaaaaa?

Post 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.
-Chris

YouTube | Twitter | Rad Raygun

“REAL ARTISTS SHIP” - Steve Jobs
Post Reply