【发布时间】:2011-12-07 18:18:46
【问题描述】:
我正在尝试定义一个指向成员函数的“通用”指针,该指针可用于访问特定类中的各种成员函数,如下所示:
class Security{
inline std::vector<double> member_function(const std::string &input_data_string){ return ... a vector<double>....;};
};
我正在定义一个指向成员函数的通用指针,该成员函数可用于通过使用 boost::variant 访问“其他”函数以及具有不同返回类型和不同类型参数的函数。
typedef boost::variant<std::string,double, std::vector<double>, std::vector<std::string>> (Security::*ptr_sec_fn)(boost::variant<std::string,double, std::vector<double>, std::vector<std::string>> );
然后将指针赋值给成员函数
ptr_sec_fn=&Security::member_function;
这是我得到的错误:
cannot convert from 'std::vector<_Ty> (__thiscall Security::* )(const std::string &)' to 'boost::variant<T0_,T1,T2,T3> (__thiscall Security::* )(boost::variant<T0_,T1,T2,T3>)'
1> with
1> [
1> _Ty=double
1> ]
1> and
1> [
1> T0_=std::string,
1> T1=double,
1> T2=std::vector<double>,
1> T3=std::vector<std::string>
1> ]
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
你能帮我找出我在这里做错了什么吗?非常感谢。
【问题讨论】:
-
您是否考虑过改用
boost::function或std::function? -
另请查看
boost::bind和boost::phoenix::bind
标签: c++ pointers boost casting