Is there a way to create a collection of Method objects, for example a
vector or a list?
template <typename elemType>
struct Method
{
bool (*function)(ele mType&);
template <typename elemType>
inline bool operator()(elem Type & property)
{
return function(proper ty);
}
};
so that I can do something like:
int main()
{
std::string property;
std::vector <MethodmethodLi st;
Method method;
method.function = someMethod;
methodList.push _back(method);
method.function = someOtherMethod ;
methodList.push _back(method);
std::vector<Met hod>::iterator it;
for(it = methodList.begi n(); it != methodList.end( ); it++)
{
std::cout << "Value: " << ((*it)(property )) << std::endl;
}
return 0;
}
bool someMethod(int & value)
{
value = 25;
return true;
}
bool someOtherMethod (std::string & value)
{
value = "Hello, World!";
return true;
}
vector or a list?
template <typename elemType>
struct Method
{
bool (*function)(ele mType&);
template <typename elemType>
inline bool operator()(elem Type & property)
{
return function(proper ty);
}
};
so that I can do something like:
int main()
{
std::string property;
std::vector <MethodmethodLi st;
Method method;
method.function = someMethod;
methodList.push _back(method);
method.function = someOtherMethod ;
methodList.push _back(method);
std::vector<Met hod>::iterator it;
for(it = methodList.begi n(); it != methodList.end( ); it++)
{
std::cout << "Value: " << ((*it)(property )) << std::endl;
}
return 0;
}
bool someMethod(int & value)
{
value = 25;
return true;
}
bool someOtherMethod (std::string & value)
{
value = "Hello, World!";
return true;
}
Comment