从相同的父函数调用不同的子函数

时间:2022-08-07 23:56:18

Im looking to have a common parent function like so

我希望有一个像这样的共同父功能

void main (param 1, param 2)
{
    <stuff to do>
    param1();
    print("The function %s was called", param 2);
    <more stuff to do>
}

Where param 1 will be the name of the function to be called and param 2 will be some descriptive text. param 2 is easy and I have that solved, but Im unclear as to how I would call a function from the same parent function by passing in the functions name. THere are a few other things that the parent function does, but instead of having multiple parent function who only differ in the function they call or a single parent with a switch statement, id prefer if this way was possible. Any thoughts?

其中param 1将是要调用的函数的名称,param 2将是一些描述性文本。 param 2很简单,我已经解决了,但我不清楚如何通过传入函数名称从同一个父函数调用函数。这是父函数所做的一些其他事情,但不是让多个父函数只在他们调用的函数或带有switch语句的单个父函数上有所不同,如果这种方式可行,则id更喜欢。有什么想法吗?

5 个解决方案

#1


What you actually want to pass for param 1 is a function pointer to the function you would like to call, as opposed to that function's name.

你实际想要为param 1传递的是一个指向你想要调用的函数的函数指针,而不是该函数的名称。

Have a look at this tutorial about function pointers.

看看这个关于函数指针的教程。

#2


What language is this? if functions aren't first-class citizens in this language, you can't do it

这是什么语言?如果函数不是这种语言的一等公民,你就做不到

If it's something like Java, you could get around this by passing in an object that implements an interface, and just call that function on the object.

如果它类似于Java,你可以通过传入一个实现接口的对象来解决这个问题,并在对象上调用该函数。

#3


I'd say that function pointers are a good thing but you may be able to leverage the pre-processor to do what you want...(learning how to use function pointers is a good thing though)...

我会说函数指针是一件好事,但你可以利用预处理器做你想做的事情......(学习如何使用函数指针虽然是好事)...

I mean, if param2 is something that is known statically, it's pretty useless to have function pointers.. Function pointers would be better if you've dynamic values and while running your program once you want to give param2 = foo and then param2 = bar...

我的意思是,如果param2是静态已知的东西,那么拥有函数指针是没有用的。如果你有动态值,那么函数指针会更好,一旦你想给param2 = foo然后运行你的程序,那么函数指针会更好。然后param2 = bar ...

Do you actually need different function prototypes or could you have something like...

你真的需要不同的功能原型,或者你能不能拥有......

#ifdef BAR
 void foo() { whatever(); }
#elif defined(FOO)
 void foo() { whocares(); }
#else
 #define foo() do{} while(0)
#endif

and always have the same body but giving different -D on the command line...

并且始终具有相同的主体但在命令行上给出不同的-D ...

#4


Something like this?

像这样的东西?

#include <stdio.h>
void foo(void) { printf("foo\n"); }
void bar(void) { printf("bar\n"); }
static inline void parent(void func(), const char *msg)
{
    /* do stuff */
    func();
    printf("%s called\n", msg);
    /* more stuff */
}
int main(void)
{
    parent(foo, "test1");
    parent(bar, "test2");
    return 0;
}

#5


This is not easy to do in C and compiled languages in general. On Windows I have something like this ( its been a long time ):

一般来说,这在C语言和编译语言中并不容易。在Windows上,我有类似的东西(很长一段时间):

typedef void(* simple_void_function )()
HMODULE exe_module = GetModuleHandle( NULL ); // not sure about this
simple_void_function = GetProcAddress( exe_module, param1 );
(*simple_void_function)();

The actual function call signature can be more flexible, if you are willing to manually push args onto the stack.

如果您愿意手动将args推入堆栈,则实际的函数调用签名可以更灵活。

#1


What you actually want to pass for param 1 is a function pointer to the function you would like to call, as opposed to that function's name.

你实际想要为param 1传递的是一个指向你想要调用的函数的函数指针,而不是该函数的名称。

Have a look at this tutorial about function pointers.

看看这个关于函数指针的教程。

#2


What language is this? if functions aren't first-class citizens in this language, you can't do it

这是什么语言?如果函数不是这种语言的一等公民,你就做不到

If it's something like Java, you could get around this by passing in an object that implements an interface, and just call that function on the object.

如果它类似于Java,你可以通过传入一个实现接口的对象来解决这个问题,并在对象上调用该函数。

#3


I'd say that function pointers are a good thing but you may be able to leverage the pre-processor to do what you want...(learning how to use function pointers is a good thing though)...

我会说函数指针是一件好事,但你可以利用预处理器做你想做的事情......(学习如何使用函数指针虽然是好事)...

I mean, if param2 is something that is known statically, it's pretty useless to have function pointers.. Function pointers would be better if you've dynamic values and while running your program once you want to give param2 = foo and then param2 = bar...

我的意思是,如果param2是静态已知的东西,那么拥有函数指针是没有用的。如果你有动态值,那么函数指针会更好,一旦你想给param2 = foo然后运行你的程序,那么函数指针会更好。然后param2 = bar ...

Do you actually need different function prototypes or could you have something like...

你真的需要不同的功能原型,或者你能不能拥有......

#ifdef BAR
 void foo() { whatever(); }
#elif defined(FOO)
 void foo() { whocares(); }
#else
 #define foo() do{} while(0)
#endif

and always have the same body but giving different -D on the command line...

并且始终具有相同的主体但在命令行上给出不同的-D ...

#4


Something like this?

像这样的东西?

#include <stdio.h>
void foo(void) { printf("foo\n"); }
void bar(void) { printf("bar\n"); }
static inline void parent(void func(), const char *msg)
{
    /* do stuff */
    func();
    printf("%s called\n", msg);
    /* more stuff */
}
int main(void)
{
    parent(foo, "test1");
    parent(bar, "test2");
    return 0;
}

#5


This is not easy to do in C and compiled languages in general. On Windows I have something like this ( its been a long time ):

一般来说,这在C语言和编译语言中并不容易。在Windows上,我有类似的东西(很长一段时间):

typedef void(* simple_void_function )()
HMODULE exe_module = GetModuleHandle( NULL ); // not sure about this
simple_void_function = GetProcAddress( exe_module, param1 );
(*simple_void_function)();

The actual function call signature can be more flexible, if you are willing to manually push args onto the stack.

如果您愿意手动将args推入堆栈,则实际的函数调用签名可以更灵活。