条件函数定义 [英] Conditional Function definition

查看:73
本文介绍了条件函数定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很长的while循环代码.在此while循环中,有一个很长的切换用例函数,以便知道应在bar上应用哪个函数.

I have a code with a very long while loop. In this while loop, there is a long switch-case function in order to know which function should be applied on bar.

int i=0;
while(i<BigNumber)
{
   switch(variable)
   {
      case 1:
          foo1(bar);
      case 2:
          foo2(bar);
      etc...
   }
   i = i+1;
}

但是,从while循环的一次迭代到另一次迭代,switch-case结构中的大小写始终相同.因此,我认为在while循环之前定义一个唯一函数foo应用于bar会更明智,但是我可以对foo进行条件定义吗?像

However, from one iteration to another of this while loop, the case in the switch-case structure is always the same. I would therefore, think that it would be more intelligent to define a unique function foo to apply on bar before the while loop but can I make this conditional definition of foo? Something like

switch(variable)
{
   case 1:
      define foo this way
   case 2:
      define foo that way
   etc...
}

int i=0;
while(i<BigNumber)
{
   foo(bar);
   i = i+1;
}

但是不能在main中定义一个函数,因此此代码无法编译.我该如何解决这个问题?有什么方法可以定义以某条件为条件的函数吗?最好的解决方案是否包括建立指向函数的指针数组,然后通过执行类似(*myarray[variable])(bar)的操作在while循环中调用正确的函数?

but one cannot define a function within main, so this code fails to compile. How can I solve this issue? Is there any way to define a function conditional to something? Does the best solution consists of making an array of pointers to functions and then call the correct function within the while loop by doing something like (*myarray[variable])(bar)?

推荐答案

在这里,函数指针是为解决此类挑战而量身定制的.您将定义常规功能foo1foo2,以及可能需要的其他功能.然后,使用语法return type (*fpointername)(arg, list);定义function pointer.arglist参数只是要分配的功能的type. (例如,如果您的函数是void foo1 (int a, char b),则您将函数指针声明为void (*fpointername)(int, char);(fn指针定义中省略了变量名).

Here, function pointers are tailor-made to solve this type of challenge. You will define your normal functions foo1, foo2, and any more you may need. You then define a function pointer with the syntax return type (*fpointername)(arg, list); Your arg, list parameters are just the type for the functions to be assigned. (e.g if your functions were void foo1 (int a, char b) then you declare your function pointer as void (*fpointername)(int, char); (variable names omitted in fn pointer definition).

以下是说明这一点的简单程序.如有任何疑问,请发表评论:

The following is a simple program that illustrates the point. Drop a comment if you have any questions:

#include <stdio.h>

/* 3 void functions, one of which will become foo
 * dependeing on the outcome of the switch statement
 */
void foo1 (char c) {
    printf ("\n  %s(%c)\n\n", __func__, c);
}

void foo2 (char c) {
    printf ("\n  %s(%c)\n\n", __func__, c);
}

void foodefault (char c) {
    printf ("\n  %s(%c)\n\n", __func__, c);
    printf ("  Usage: program char [a->foo1, b->foo2, other->foodefault]\n\n");
}

/* simple program passes argument to switch
 * statement which assigns function to function
 * pointer 'foo ()' based on switch criteria.
 */
int main (int argc, char **argv) {

    void (*foo)(char) = NULL;               /* function pointer initialized to NULL */
    char x = (argc > 1) ? *argv[1] : 'c';   /* set 'x' value depending on argv[1]   */

    switch (x)                              /* switch on input to determine foo()   */
    {
        case 'a' :                          /* input 'a'                            */
            foo = &foo1;                    /* assign foo as foo1                   */
            break;
        case 'b' :                          /* input 'b'                            */
            foo = &foo2;                    /* assign foo as foo2                   */
            break;
        default :                           /* default case assign foo foodefault   */
            foo = &foodefault;
    }

    foo (x);                                /* call function foo(x)                 */

    return 0;
}

输出:

$ ./bin/foofn

  foodefault(c)

  Usage: program char [a->foo1, b->foo2, other->foodefault]

$ ./bin/foofn a

  foo1(a)

$ ./bin/foofn b

  foo2(b)

$ ./bin/foofn d

  foodefault(d)

  Usage: program char [a->foo1, b->foo2, other->foodefault]

这篇关于条件函数定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆