By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,490 Members | 1,489 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,490 IT Pros & Developers. It's quick & easy.

use class member function as a parameter

P: n/a
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
Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
xuatla wrote:
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); } ;
Jul 23 '05 #2

P: n/a
Rolf Magnus wrote:
xuatla wrote:

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


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

Jul 23 '05 #3

P: n/a
xuatla wrote:
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.

Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.