Skip to main content
gamedev.net
🔒 Locked

c++ pointer question

Started by game mercenary 4 replies 550+ views
Original Post
game mercenary
game mercenary
I have a struct with int *handle() in it, I need it to point to another function so when I call handle() it will execute that other function. am I correct that *handle() = otherfunc(); is what I need to do?
jamdon_ms
jamdon_ms
If you want a pointer to a function, you'll need to add some parentheses in. Right now, your function prototype returns a pointer to int.

int *handle() is the same as int* handle()

int (*handle)() is a pointer to a function that takes no parameters and returns an int. If you want to assign another function to this pointer, just make sure you have the same function prototype. So if you have a function such as

int foo(), you could do this:

handle = foo;
Julian Spillane
Julian Spillane
Actually, polymorphism has very little to do with this situation. What you need to do is specify your "handle()" procedure as a function pointer. I was about to go into the details regarding it, but I just realized that "jamdon_ms" beat me to it. :p Oh well. Serves me right for being slow on the draw.
Nokturnal
Nokturnal
Here is something to get you started on function pointers

www.function-pointer.org/
"I think there is a world market for maybe five computers." -- Thomas Watson, Chairman of IBM, 1943
game mercenary
game mercenary
I will look into that, thanks. this is my first practacal use of pointers outside of loading a BMP in SDL so I dont know to much about other uses.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.