V.S.
V.S.

Reputation: 3004

C++ function pointers and presetting arguments

Say I have a function 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?

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).

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). 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.

Upvotes: 0

Views: 266

Answers (1)

Miserable Variable
Miserable Variable

Reputation: 28752

I don't see why you can do this if you have defined the function g points to as

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

The better way to do this in C++ is probably using functors.

Upvotes: 3

Related Questions