void func()
{
}
void* fp;
fp = &func
//and to use it
(*fp)();
Another thing is i have no idea how a function is stored...is it like a variable?...Thanks for any input!
-blide
blide@mail.com
Pointers to functions
Hi all,
I have heard somethings about pointers to functions and I am wondering what that is and how it works. Could someone please tell how to make a pointer to a function and why it would ever be useful, thanks!
My guess is it would look like making a normal pointer but I have no idea and I still don''t know how to use it.
My idea:
-blideblide@mail.com
Hi,
I guess it''s an alternative to virtual functions. I think pointers to functions are faster than virtual functions, (correct me if I''m wrong).
That is ONE usage of them!
/Mankind gave birth to God.
I guess it''s an alternative to virtual functions. I think pointers to functions are faster than virtual functions, (correct me if I''m wrong).
That is ONE usage of them!
/Mankind gave birth to God.
/Mankind gave birth to God.
blide -
Thinking in terms of C and not C++, funtion pointers have severl uses. A common use is as a callback function. A good example of this is the WindowProcedure you have to write to handle window messages. Or, say your writing a multi-purpose graphics library. In order to handle different color depths you could have a function pointer for each routine that would be different at each depth. For example, Put_Pixel pointer and the functions Put_Pixel15, Put_Pixel16, etc...
As to declaring and using functions, your syntax is a bit off.
Both styles of calling functions through pointers are acceptable in ANSI C, but K & R C only uses the first. Not a big deal these days.
DDNewbie -
In C++, pointers *are* an alternative to virtual functions, and in most case virtual functions suit you better if you are working with classes. For example, using the graphics lib example above, you could define a pure virtual base class called CGraphics, and inherit from that with CGraphics15, CGraphics16, etc...
Thinking in terms of C and not C++, funtion pointers have severl uses. A common use is as a callback function. A good example of this is the WindowProcedure you have to write to handle window messages. Or, say your writing a multi-purpose graphics library. In order to handle different color depths you could have a function pointer for each routine that would be different at each depth. For example, Put_Pixel pointer and the functions Put_Pixel15, Put_Pixel16, etc...
As to declaring and using functions, your syntax is a bit off.
// declarationvoid (*fp1)(void);int (*fp2)(char *arg);// functionsvoid func1 (void){}int func2 (char *arg){ return 0;}// assignmentfp1 = func1;fp2 = func2;// usage// K&R style(*fp1)();(*fp2)("Hello");// ANSIfp1 ();fp2 ("Hello");
Both styles of calling functions through pointers are acceptable in ANSI C, but K & R C only uses the first. Not a big deal these days.
DDNewbie -
In C++, pointers *are* an alternative to virtual functions, and in most case virtual functions suit you better if you are working with classes. For example, using the graphics lib example above, you could define a pure virtual base class called CGraphics, and inherit from that with CGraphics15, CGraphics16, etc...
--- Official D Blog | Learning D | The One With D | D Bits
Another use is when wanting to pass a pointer to a function in as part of the parameter list for another function.
A good example of this is in the qsort function in C. You pass in a pointer to the compare function.
-----------------------------------------------
All messages are of my own personal opinion and
not meant to offend. But if they do - tough :)
Neuro.
A good example of this is in the qsort function in C. You pass in a pointer to the compare function.
-----------------------------------------------
All messages are of my own personal opinion and
not meant to offend. But if they do - tough :)
Neuro.
-----------------------------------------------All messages are of my own personal opinion and not meant to offend. But if they do - tough :)Neuro.
Pointers to functions can be used in various ways. By far, one of the most powerful being an array of pointers to functions.
-----------------------------
A wise man once said "A person with half a clue is more dangerous than a person with or without one."
-----------------------------
A wise man once said "A person with half a clue is more dangerous than a person with or without one."
-----------------------------A wise man once said "A person with half a clue is more dangerous than a person with or without one."The Micro$haft BSOD T-Shirt
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement