I am writing a program that performs a basic set of operations, but allows the user to fill in the specific functions that are called (and they choose those functions before compiling). For example, my program may call a function filter(input,&output)
but the user can write their own filter.
The ways I've read about that may solve this problem are function pointers and virtual functions. It looks like I can either do something along the lines of
int (*pt2Filter)(float,&float) = NULL;
int IIRFilter(float input, float &output);
pt2Filter=&IIRFilter;
for a function pointer. But that doesn't let me keep track of internal states in the filter.
Or I could make a class myClass
with a virtual filter
function, and then the user would make an IIR
class that inherits from myClass
and overwrites the filter
function.
class myClass
{
virtual void Filter(float input, float &output);
...
};
class IIR : public myClass
{
float stateVariable;
virtual void Filter(float input, float &output);
}
void IIR::Filter(float input, float &output)
{ //IIR filter done here }
I guess my question is how do I call the filter function from my program without knowing that an IIR
class even exists?
Or if I'm going about this completely wrong, how do I go about calling my Filter
function when my goals are to 1: let the user define whatever filter they want. 2: Don't allow the user to change my source code
Update This may have not been as difficult as I first thought. I created a header file where the users will say which function they want the Filter class to call using the line
//User types this into "FunctionImplementations.h"
#include "IIR.h"
typedef IIR FilterImplementation;
//then I just type
#include "FunctionImplementations.h"
FilterImplementation.filter(); //Implements IIR classes filter function