Practical use for pointers to functions in C++?

Started by
15 comments, last by Iron Eye 20 years, 10 months ago
quote:Original post by Village Specialton
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.


Every language uses pointers. A pointer is nothing but a reference to a location in memory. How they abstract these pointers to pretend they don''t exist is highly irrelevant.


quote:
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.


I really don''t want to comment on this remark. I''ll just quote it. This deserves to be quoted and perhaps even framed. I love gamedev.
Advertisement
quote:Original post by Ready4Dis
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.


No, C++ style prefers using references/pointers to virtual classes for this.

How appropriate. You fight like a cow.
typedef void func();void foo() { /* ... */ }void bar() { /* ... */ }struct entry{  const char* keyword;  func* function;};const entry table[] ={  "foo", do_foo,  "bar", do_bar,  /* more pairs keyword-function pointer */  0, 0};void execute( const std::string& command ){  entry* ptr = table;  while( ptr->keyword && ptr->keyword != command )    ++ptr;  if( ptr->function )    ptr->function();}


Pass a keyword to execute and it will call the corresponding function.

edit : forgot a few const.

[edited by - Fruny on June 19, 2003 5:48:29 PM]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
might be a good idea to use a map or hashmap there.... O(n) -> O(log n).

How appropriate. You fight like a cow.
quote:Original post by Sneftel
might be a good idea to use a map or hashmap there.... O(n) -> O(log n).


I know. I can''t be bothered to tonight.

[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Shouldn''t a hash_map be O(1) unless you have a really sucky hash function?
I''m surprised nobody has mentioned this yet

Pointers t functions are also used for accessing the newer features of OpenGL (the extensions). As you know, Windows only supports OpenGL 1.1. If you want to us any of the neat newer functions of OpenGL 1.4, you will need pointers-to-functions to access them.

Sander Maréchal
[Lone Wolves Game Development][RoboBlast][Articles][GD Emporium][Webdesign][E-mail]


GSACP: GameDev Society Against Crap Posting
To join: Put these lines in your signature and don''t post crap!

<hr />
Sander Marechal<small>[Lone Wolves][Hearts for GNOME][E-mail][Forum FAQ]</small>

This topic is closed to new replies.

Advertisement