【发布时间】:2012-09-06 15:21:00
【问题描述】:
我在超类中有函数,旨在将字符串与内部函数相关联:
class Base
{
typedef std::function<void(double)> double_v;
bool registerInput(std::string const& key, double_v const& input) {
functions[key] = input;
}
void setInput(std::string key, double value) {
auto fit = functions.find(key);
if (fit == functions.end()) return;
fit->second(value);
}
std::map<std::string, double_v> functions;
}
想法是我可以注册函数的任何子类都可以用字符串和值调用它们:
SubBase::SubBase() : Base(){
Base::registerInput(
"Height",
static_cast<void (*)(double)>(&SubBase::setHeight)
);
}
void SubBase::setHeight(double h) {
....
}
然后可以调用:
subBaseInstance.setInput("Height", 2.0);
但是当我编译时出现以下错误:
In constructor ‘SubBase::SubBase()’
error: invalid static_cast from type ‘<unresolved overloaded function type>’ to type ‘void (*)(double)’
我错过了什么?
【问题讨论】:
-
成员函数不是函数。这行不通。
标签: c++ casting overloading