Connecting Tech Pros Worldwide Forums | Help | Site Map

use class member function as a parameter

xuatla
Guest
 
Posts: n/a
#1: Jul 23 '05
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 )
{ 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.

I know that I need to read some comprehensive materials about this part.
But can I get some help from you guys here first? I will also be very
appreciated if you point out some reference to me.

Thank you.

-xuatla

Rolf Magnus
Guest
 
Posts: n/a
#2: Jul 23 '05

re: use class member function as a parameter


xuatla wrote:
[color=blue]
> 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=blue]
> { 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); } ;


xuatla
Guest
 
Posts: n/a
#3: Jul 23 '05

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]
Rolf Magnus
Guest
 
Posts: n/a
#4: Jul 23 '05

re: use class member function as a parameter


xuatla wrote:
[color=blue]
> The problems remained are:
> 1. Since B will be different classes, can I define a template member
> function in a non-template class?[/color]

Yes.
[color=blue]
> class A
> {
> template <class B> evaluate( B& .... ) ...
> }
> If yes, how to call it?
> evaluate( b, b::func, x); (?)
> evaluate( *this, *this::func, x ); (?)[/color]

evaluate(b, B::func, x);

or

evaluate(b, b.func, x);

[color=blue]
> 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)?[/color]

Not sure what you mean here. The name of the pointer isn't related to the
name of the function it points to.

Closed Thread