I have a simple code at down:
int global1(int x)
{
return x * 5;
}
int global2(int x)
{
return x + 5;
}
struct some_struct {
int a;
int foo1(int x)
{
return x * a;
}
int foo2(int x)
{
return x + a;
}
int bar1(int x, bool a)
{
auto func = a ? global1 : global2; // works
// auto func = a ? &global1 : &global2; // also works
return func(x);
}
int problem_function(int x, bool a) // It is my question
{
auto func = a ? foo1 : foo2; // gives error
// auto func = a ? &foo1 : &foo2; // also gives error
// auto func = a ? &this->foo1 : &this->foo2; // also gives error
return func(x);
}
};
It is very simplyfied form of the code in the real code i have no way to carry function on outside like how i did in this with global1() & global2()
I wanna call one of functions in the struct but using a pointer to a function but it gives error.
Note: I cant use if else becouse in the real code its not returns func(x) in real code i use func(x) as a condition for a function in a loop (I am not joking its literaly what i said)
A part from real code:
void* find_first(ExprToken* (*cond)(bool (*)(ExprToken*))) {...} // yeah
I know if I want to call a function of struct I have to tell which variable(struct) I am using to compiler but how.
Is even C++ supports function pointer in struct?
Egemen Yalın is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.