use class member function as a parameter

X

xuatla

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
 
R

Rolf Magnus

xuatla said:
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 )

The first parameter is not a pointer to member.
{ 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.

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); } ;
 
X

xuatla

Rolf said:
xuatla wrote:




The first parameter is not a pointer to member.




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); } ;

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
 
R

Rolf Magnus

xuatla said:
The problems remained are:
1. Since B will be different classes, can I define a template member
function in a non-template class?
Yes.

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

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

or

evaluate(b, b.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)?

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,241
Latest member
Lisa1997

Latest Threads

Top