alternatives to pointers to member functions

I

Ian Malone

I have a class which looks like this:

class reco {
public:
int height, width;
double beta;
double mu;

simple_d_field estimate;
simple_d_field auxiliary;
simple_d_field data;

reco() : { resize(0,0); data.resize(0,0);}

virtual void resize (int height, int width);

virtual double potential(int site) =0; /* Returns total potential for
site due to neighbours */
virtual double gradient(int site) =0; /* Returns sum of potential due
to each neighbour differentiated
* wrt site value. */
virtual void init_estimate(double);
virtual void update_aux(void) =0;
virtual void update_est(void) =0;

virtual ~reco();
};

Where simple_d_field is a class containing a vector of
doubles and member functions to load & save. The idea
is that the user creates an instance of an inheriting
class (which has definitions of all those virtual
members) and alternately calls update_aux(), update_est()
to update auxiliary based on data, then estimate based
on auxiliary. This is fine.

However I've got a choice of functions for potential(),
gradient() and the others, and I'd like to be able to mix
and match (without creating all the possible permutations
of classes, which is a little difficult to maintain).
In C I'd use function pointers, but these functions need
access to class members (as do some more in inheriting
classes), and I'd like to keep the class encapsulation.
So I need to use pointers to member functions.

That looks a little nightmarish: are there any alternatives
I should consider?
 
I

Ian Malone

There is of course an easier and better way than function pointers in
C++. What you need is templates, and functors. Here is an article that
deals with exactly the problem you are describing.
http://www.codeproject.com/cpp/mem_fun_bind.asp

Hope that helps,
Philip

Thanks very much. It took me a while to see how it worked (I had to
read the linked threads because the iterator part was throwing me off).
It looks like this does exactly what I was asking for, but it's
actually put me onto a slightly different approach using structs with
overloaded operator() as members of the class.
 
J

Jonathan Mcdougall

However I've got a choice of functions for potential(),
gradient() and the others, and I'd like to be able to mix
and match (without creating all the possible permutations
of classes, which is a little difficult to maintain).
In C I'd use function pointers, but these functions need
access to class members (as do some more in inheriting
classes), and I'd like to keep the class encapsulation.
So I need to use pointers to member functions.

Have a look at Loki's Functor class (from Alexandrescu in Modern C++
Design).or Boost.Function.

Jonathan
 
I

Ian Malone

Jonathan said:
Have a look at Loki's Functor class (from Alexandrescu in Modern C++
Design).or Boost.Function.

Jonathan

Thanks very much for the suggestion. I'll definitely
have a look at it, functors as described by the link
Philip Goh posted look quite powerful, and I'll
probably find a use for them at some point. But as
I said in my other post I've decided on a simpler
approach, basically because I want to 'plug in'
functions to be used by the member functions.

Looking at that class again, double potential(int site)
and double gradient(int site) should be protected, and
they're functions I want to replace freely, so either
C-style function pointers or overloaded operator() is
going to be more appropriate (otherwise the class
definition will be constantly rewritten, I'm not very
experienced in C++, but that seems bad). I may still
use functors for the void update_aux(void),
void update_est(void) functions.
 

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

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top