| re: use class member function as a parameter
Rolf Magnus wrote:[color=blue]
> xuatla wrote:
>
>[color=green]
>>Hi,
>>
>>I have a problem about using a class member function as a parameter in
>>another function.
>>
>>What I tried to implement is something like described below:
>>
>>class A
>>{
>>public:
>>double evaluate( double (*func)(double), double x )[/color]
>
>
> The first parameter is not a pointer to member.
>
>[color=green]
>>{ return func(x); } ;
>>...
>>} ;
>>
>>class B
>>{
>>private:
>>double func1( double a )
>>{ return 1.0; } ;
>>
>>double func2( double a )
>>{ ... } ;
>>
>>public:
>>int compute(double a)
>>{
>>evaluate( func1, a ); // (***)
>>...
>>}
>>} ;
>>
>>I have problem for the above (***) part.
>>I also tried
>>evaluate( B::func1, a );
>>evaluate( (*this)::func1, a );
>>evaluate( this::func1, a );
>>but none of them works.[/color]
>
>
> You have to change the function. For one, it has to take a pointer to member
> instead of a normal function pointer, and second, you have to provide an
> object on which the member function gets called, e.g.:
>
> double evaluate(B& object, double (B::*func)(double), double x )
> { return object.func(x); } ;
>[/color]
Thank you very much!
The problems remained are:
1. Since B will be different classes, can I define a template member
function in a non-template class?
class A
{
template <class B> evaluate( B& .... ) ...
}
If yes, how to call it?
evaluate( b, b::func, x); (?)
evaluate( *this, *this::func, x ); (?)
2. I suppose that in (B::*func), "func" can be different with the name
of member function in B. But if so, then how to compile object.func(x)?
-xuatla[color=blue]
>[/color] |