标题: a small question about C++ [打印本页] 作者: yalc 时间: 2002-3-26 12:05 标题: a small question about C++ How to define a function which return a function pointer?
It may not very useful, just for fun.
If someone know, please tell me!
Thank you!作者: notodog 时间: 2002-3-26 12:25
<blockquote><font size="1" face="Arial,宋体">code:<hr /><pre style="font-size:x-small; font-family: monospace;">// Define a function pointer type
typedef void* (*func_crap)(void*);
// An instance of function
inline void* crap_one(void* boo) {
cout << "Crap No. 1" << endl;
return NULL;
}
// Here it comes:
inline func_crap func_vomitter(int hit_me_then_i_m_gonna_vomit) {
return crap_one; // or some other crap
}</pre><hr /></blockquote><font size="2" face="Arial,宋体">Actually, returnning a function pointer could sometimes be rather useful, in cases like finite state machine, dynamic function dispatcher, etc. Remove the "inline" directive should you wanna use it in cpp source file (not header).作者: yalc 时间: 2002-3-26 16:21
notodog :
Thank you very much!作者: Kang Wen Hao 时间: 2002-4-3 01:16
Dear Notodog:
I can tell for sure that you are good at C++, but not very good. Please read the following:
Complicated declarations & definitions
As an aside, once you figure out how the C and C++ declaration syntax works you can create much more complicated items. For instance:
int main() {} ///:~
Walk through each one and use the right-left guideline to figure it out. Number 1 says “fp1 is a pointer to a function that takes an integer argument and returns a pointer to an array of 10 void pointers.”
Number 2 says “fp2 is a pointer to a function that takes three arguments (int, int, and float) and returns a pointer to a function that takes an integer argument and returns a float.”
If you are creating a lot of complicated definitions, you might want to use a typedef. Number 3 shows how a typedef saves typing the complicated description every time. It says “An fp3 is a pointer to a function that takes no arguments and returns a pointer to an array of 10 pointers to functions that take no arguments and return doubles.” Then it says “a is one of these fp3 types.” typedef is generally useful for building complicated descriptions from simple ones.
Number 4 is a function declaration instead of a variable definition. It says “f4 is a function that returns a pointer to an array of 10 pointers to functions that return integers.”
You will rarely if ever need such complicated declarations and definitions as these. However, if you go through the exercise of figuring them out you will not even be mildly disturbed with the slightly complicated ones you may encounter in real life.作者: notodog 时间: 2002-4-3 12:13
Hi buddy,
Sorry for my ignorance, but, I'm not sure what's your point here. So:
1. If you are just kindly trying to help me with my ignorance, I'd sincerely thank you for text-book copying.
2. If you are trying to outsmart me, I'd humbly assume that you are, it's not always true in real though.
3. If you are helping the the other guy, I think, could it be better to copy more including how typedef works and some more two-penny tricks and academic craps? Or, to save your work, just the URL will do.
4. If you just wanna flame me, sorry, I don't argue with others.
Thanks作者: yalc 时间: 2002-4-5 07:50
Oh,
Thank you very much!
After reading notodog's note, I thought that I can't difine it without using typedef. But now, I am very clear.作者: yalc 时间: 2002-4-9 08:32
Dear notodog:
If something offending you, I am really sorry!
You may much better than I can conceive. Actually, to be a very good programmer is not easy.
Again, sorry!作者: Kang Wen Hao 时间: 2002-4-9 11:37
Sorry notodog:
I just saw the note.
It should be me to say sorry to you.
Maybe something affronting you, don't think more.
I am sincerely sorry!