I would like to implement, in c++, a class b where it would be possible to do some kind of iteration through a member set encapsulating the type of that iterator. Like:
b_object.for_each_x_do(function_f);
so function_f would get everyone of the x members and do anything. Let's say:
void function_f(x_member_type x){ cout << x << endl; }
Ok. So I'm trying to achieve that through a code like:
class b{
int *x;
public:
void foreach_x_do(void (*f)(int)){
while(*x++) // or any kind of iteration through x
f(*x);
}
};
class a{
b b_inst;
public:
void f(int x) { }
a(){
b_inst.foreach_x_do(f); // by mistake it was b_inst.foreach_x_do(f)(), however this wasn't the point at all.
}
~a(){}
};
int main(){}
However, I'm getting this error, compile-time:
fp.cpp: In constructor
‘a::a()’:
fp.cpp:17: error: no matching function for call to‘b::foreach_x_do(<unresolved overloaded function type>)’
fp.cpp:6: note: candidates are:void b::foreach_x_do(void (*)(int))
Anybody can help getting it to compile?
fyou are passing toforeach_x_dois not avoid(*)(int)it is avoid (a::*)(int).class Binstead ofclass b. Just to conform to common ussage.