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

alternatives to pointers to member functions

P: n/a
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?

--
imalone
Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


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

Jul 23 '05 #2

P: n/a
ph********@macosx.com wrote:
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.

--
imalone
Jul 23 '05 #3

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

Jul 23 '05 #4

P: n/a
Jonathan Mcdougall wrote:
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


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.

--
imalone
Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.