How can I have generic function pointer. Consider following class.
#ifndef PERSON_HPP_
#define PERSON_HPP_
#include <string>
class Person {
public:
Person() {
}
void SetName(std::string person_name) {
m_name = person_name;
}
void setDept(std::string dept_name) {
m_dept = dept_name;
}
void setAge(int person_age ) {
m_age = person_age;
}
std::string getName() {
return m_name;
}
std::string getDept() {
return m_dept;
}
int getAge() {
return m_age;
}
private:
std::string m_name;
std::string m_dept;
int m_age;
};
#endif
I want to store the function pointers in a std::vector for setName, setDept , constructor and so on ...
For normal function I could achieve this using following
#include <vector>
int mult(int a) {
return 2*a;
}
int main()
{
int b;
std::vector<void *(*)(void *)> v;
v.push_back((void *(*)(void *))mult);
b = ((int (*)(int)) v[0])(2); // The value of b is 2.
return 0;
}
No Boost allowed in my case.
If you really need to cast the member pointers to a common type (like void*), you can do:
However, I do not believe it is wise to do so. C++ has type safety for a reason.