I am working with a template class which parses data. Each line of data will require calling one of two functions to handle the data. This decision is determined at the time the parser is constructed and depends on variables passed to the constructor. I thought it would be useful to use a function pointer for this so that i could use one if statement in the constructor and assign the proper function to the function pointer which will be used in the body of the program. I am getting an error which I cannot figure out and I am curious if I am using the function pointer correctly in this context.
template<class T1, class T2>
class MyClass{
protected:
void (*pDoSomething)(std::string,std::string,std::string);
void functionOne(std::string,std::string,std::string);
void functionTwo(std::string,std::string,std::string);
public:
MyClass(bool option);
void parseData();
};
templace<class T1, class T2>
MyClass<T1,T2,>::MyClass(bool option){
if (option) pDoSomething = &functionOne;
else pDoSomething = &functionTwo;
}
template<class T1, class T2>
void MyClass<T1,T2>::parseData(){
/* . . . */
while(dataToParse){
*pDoSomething(string1, string2, string3);
}
/* . . . */
}
functionOneandfunctionTwoaren't functions. They are member functions. Totally different thing.functionOneandfunctionTwostatic, and thus turn them into functions.static void functionOne(MyClass<T1,T2>* self, std::string,std::string,std::string);