Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Iron Eye

Practical use for pointers to functions in C++?

This topic is 5657 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

Is there? This C++ says they are fairly rare in C++ as classes and such perform similar functions. ---
ConPong  _//_  Google

"Games usually keep me from making my own..."
-Me

---

Share this post


Link to post
Share on other sites
Advertisement
They are pretty rare in C++, but I have a use for them still. In my GUI Objects, you can assign a function to call when the object is updated (or in a command buttons case, clicked). That way, the action will trigger the function to be called, then depending on what function you passed, it will perform whatever you want it to. There is no way around using function pointers with this, except for programming specific functions into the actual object code, which isn''t an option for anything that needs to be non-specific. In my game, I have a label that calls a function to update itself with the current time (aka, a clock), and a command button that calls a function to log in, etc. Not really a great way to do this stuff without function pointers . It''s efficient, and it gives me a great way to do specific things with generic objects, without having to touch my GUI code (matter of fact, I can even add new types of GUI objects without modifying my current GUI code, but that''s another topic by itself).

Share this post


Link to post
Share on other sites
Generally, if you are going to be deciding what function to use at runtime (like in a switch statement) and you have a bunch of functions with the same signature, using a function pointer would cut down on the amount of code to write and overall make the code more convenient to use.

unkn.Enigma1625

Share this post


Link to post
Share on other sites
C++ has its own version of function calls: function objects. A function object is defined as any class (object) that has the overloaded () operator. Here''s a good link about function calls. The article there has a pretty good explanation. Basically, there is a way to do what you want with function objects instead of function pointers.

Not to say which is better, just to let you know it''s there

-*- My cheap, ad-ridden homepage -*-


ICQ#338688331

Share this post


Link to post
Share on other sites
if your planning to load DLL files dynamically - via LoadLibrary() and GetProcAddress() you need function pointers as GetProcAddress() returns a pointer to the function you want to call so they do have a variety of uses

Share this post


Link to post
Share on other sites
I think they're invaluable. I'm obsessed with function pointers. In my engine for example, key bindings are mapped to functions. So you can do something like this:


void InputFunction( unsigned char val )
{
switch( val )
{
case DIK_LEFT:
cout << "You pressed left. Yay." << endl;
break;
case DIK_RIGHT:
cout << "You pressed right. Yay." << endl;
break;
default:
PostError("Invalid key received in InputFunction");
}
}

BindKey( DIK_LEFT, InputFunction );
BindKey( DIK_RIGHT, InputFunction );


Or if your game has multiple key maps, which it more than likely would. Then you could have a function to process the arrow keys in your game, compared to the menu, etc, etc.. all extremely trivially. Function pointers are GREAT for this. Also since the engine stores the function pointers in an array and can just reference them by pressedKeyBindings[ DIK_VALUE ]( DIK_VALUE ) its extremely efficient.


[edited by - haro on June 19, 2003 2:26:48 PM]

Share this post


Link to post
Share on other sites
I see no use for pointers. Many veterans will tell you thaqt pointers are the key to programming, but it seems languages that don''t use them get along just fine. However, pointers are good for a few things. Using pointers, you can get the address of the function. This might be useful when your programming a giant game and need to keep track of a bot''s position. Pointers also allow you to waste memory to make your code look bigger. Anyways, I would say use them if you must. I see no point in the until you get into complex AI, but it is worth a try.

Scott Simontis
Engineer in Training
Have a nice day!

Share this post


Link to post
Share on other sites
Village, do you mean that pointers, in general, are useless? Without pointers it would be pretty difficult to write OOP sourcecode.

------------------------------
Suffering binds you to reality.

Share this post


Link to post
Share on other sites
quote:
Original post by Village Specialton
This might be useful when your programming a giant game and need to keep track of a bot''s position. Pointers also allow you to waste memory to make your code look bigger.


congratulations on making yourself look like a complete fool.

-eldee
;another space monkey;
[ Forced Evolution Studios ]


::evolve::

Do NOT let Dr. Mario touch your genitals. He is not a real doctor!

Share this post


Link to post
Share on other sites
That''s a mighty fine piece of flame bait, there, VS.




Best viewed with irony.

Hail Eris! All fnord hail Discordia!

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!