简体   繁体   English

C ++函数指针和预设参数

[英]C++ function pointers and presetting arguments

Say I have a function f(a, b, c) . 说我有一个函数f(a, b, c) Is it possible to create function pointers g and h such that when you use g and h they use a predetermined value for one of the arguments? 是否可以创建函数指针gh ,以便在使用gh它们为参数之一使用预定值?

For example (*g)(b, c) would be equivalent to f(1, b, c) and (*h)(b, c) would be equivalent to calling f(2, b, c) . 例如(*g)(b, c)将等效于f(1, b, c)(*h)(b, c)将等效于调用f(2, b, c)

The reason why I am doing this is that I am calling a function to which I can obtain a pointer (through dlsym) and this function has drastically different behaviour depending on what the first argument is (so much so that they shouldn't really be called the same thing). 我这样做的原因是,我正在调用一个可以通过其获取指针的函数(通过dlsym),并且该函数根据第一个参数的不同而具有截然不同的行为(以至于它们实际上不应该是称为同一件事)。 What's more the function pointer variable that is being used in the main program can only generally take two arguments as this is a special case where the imported function has three (but really only two are necessary, one is essentially a settings parameter). 此外,在主程序中使用的函数指针变量通常只能使用两个参数,因为这是特例,其中导入的函数具有三个参数(但实际上只有两个是必需的,一个本质上是一个设置参数)。

Thanks. 谢谢。

I don't see why you can do this if you have defined the function g points to as 我不明白为什么将g指向的函数定义为

void gfunc(int b, int c) { 
  f (1, b, c); 
}
g = &gfunc;

The better way to do this in C++ is probably using functors. 在C ++中执行此操作的更好方法可能是使用函子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM