I
inquisitive
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)(elemType&);
template <typename elemType>
inline bool operator()(elemType & property)
{
return function(property);
}
};
so that I can do something like:
int main()
{
std::string property;
std::vector <Method> methodList;
Method method;
method.function = someMethod;
methodList.push_back(method);
method.function = someOtherMethod;
methodList.push_back(method);
std::vector<Method>::iterator it;
for(it = methodList.begin(); 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)(elemType&);
template <typename elemType>
inline bool operator()(elemType & property)
{
return function(property);
}
};
so that I can do something like:
int main()
{
std::string property;
std::vector <Method> methodList;
Method method;
method.function = someMethod;
methodList.push_back(method);
method.function = someOtherMethod;
methodList.push_back(method);
std::vector<Method>::iterator it;
for(it = methodList.begin(); 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;
}