Jump to content
  • Advertisement
Sign in to follow this  
game mercenary

c++ pointer question

This topic is 4816 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
No, your handle() procedure will return a pointer, it won't point to another function. What you want to look at I think is virtual functions and polymorphism... a google search should help you out here.

Share this post


Link to post
Share on other sites
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;

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!