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?
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?