租房买房买生意上iU91
查看: 748|回复: 7
打印 上一主题 下一主题

[电脑] a small question about C++

[复制链接]   [推荐给好友]
跳转到指定楼层
楼主
发表于 2002-3-26 12:05 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
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!
2#
发表于 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 &lt;&lt; &quot;Crap No. 1&quot; &lt;&lt; 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).
回复 支持 反对

使用道具 举报

3#
 楼主| 发表于 2002-3-26 16:21 | 只看该作者
notodog :

Thank you very much!
回复 支持 反对

使用道具 举报

4#
发表于 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:

//: C03:ComplicatedDefinitions.cpp

/* 1. */     void * (*(*fp1)(int))[10];

/* 2. */     float (*(*fp2)(int,int,float))(int);

/* 3. */     typedef double (*(*(*fp3)())[10])();
             fp3 a;

/* 4. */     int (*(*f4())[10])();

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.
回复 支持 反对

使用道具 举报

5#
发表于 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
回复 支持 反对

使用道具 举报

6#
 楼主| 发表于 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.
回复 支持 反对

使用道具 举报

7#
 楼主| 发表于 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!
回复 支持 反对

使用道具 举报

8#
发表于 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!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

Copyright © 1999 - 2024 by Sinoquebec Media Inc. All Rights Reserved 未经许可不得摘抄  |  GMT-5, 2024-12-22 00:52 , Processed in 0.047922 second(s), 42 queries .