Pointer to class member as a function name

Q

QH Hong

Hi, I have several classes and several members.

class c1, c2, c3,...
member of class c1 is m11, m12,...
member of class c2 is m21, m22,...

Each member takes one argument (double type) and returns
double value.

I have a function which takes the function name and
do some calcuations and return the solution of the function.

double getsolution(memberpointer f, double x)
{
double y0 = f(x);
.... do some calculation
return yn;
}

I need

double y11 = getsolution(c1::m11, x1);
double y12 = getsolution(c1::m12, x2);
..
double y21 = getsolution(c2::m21, x1);
double y22 = getsolution(c2::m22, x2);
....

Is it possible to code like above?
How to define "memberpointer" at the definition
of getsolution?
 
J

John Carson

QH Hong said:
Hi, I have several classes and several members.

class c1, c2, c3,...
member of class c1 is m11, m12,...
member of class c2 is m21, m22,...

Each member takes one argument (double type) and returns
double value.

I have a function which takes the function name and
do some calcuations and return the solution of the function.

double getsolution(memberpointer f, double x)
{
double y0 = f(x);
.... do some calculation
return yn;
}

I need

double y11 = getsolution(c1::m11, x1);
double y12 = getsolution(c1::m12, x2);
..
double y21 = getsolution(c2::m21, x1);
double y22 = getsolution(c2::m22, x2);
....

Is it possible to code like above?
How to define "memberpointer" at the definition
of getsolution?

See

http://www.parashift.com/c++-faq-lite/pointers-to-members.html

Note that a pointer to a member of class c1 is a different type to a pointer
to a member of class c2 so your getsolution function will need to be
overloaded for each class (or at least that is the obvious solution).
 

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
474,156
Messages
2,570,878
Members
47,408
Latest member
AlenaRay88

Latest Threads

Top