我怎样才能拥有通用函数指针。考虑下课。
#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
我想将函数指针存储在std::vector
for setName
, setDept
,constructor
等等...
对于正常功能,我可以使用以下方法实现此目的
#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;
}
Boost
在我的情况下不允许。