Nik*_*hil 9 c++ function-pointers
代码段如下.无法理解为什么我收到此错误.
void
SipObj::check_each_field()
{
map <std::string, sip_field_getter>::iterator msg;
string str;
char name[20];
bool res = false;
sscanf(get_payload(), "%s %*s", name);
LOGINFO(lc()) << "INVITE:" << name;
str = name;
msg = sip_field_map.find(str);
if (msg != sip_field_map.end()) {
sip_field_getter sip_field = msg->second;
res = (this).*sip_field();
}
}
typedef bool (SipObj::*sip_field_getter)();
static map <std::string, sip_field_getter> sip_field_map;
sip_field_getter is a place holder for function names
Run Code Online (Sandbox Code Playgroud)
Jam*_*lis 21
(this).*sip_field();
Run Code Online (Sandbox Code Playgroud)
这个表达式有两个问题:
this是一个指针,因此您必须使用->*通过其上的指针调用成员函数.
函数call(())具有比逐个成员运算符(.*或者->*)更高的优先级,因此您需要使用括号来正确地对表达式进行分组.
正确的表达方式是:
(this->*sip_field)();
Run Code Online (Sandbox Code Playgroud)
Vip*_*ley 10
看起来你正在调用指针上的方法指针,但是你用点星语法调用它.尝试更换
res = (this).*sip_field();
Run Code Online (Sandbox Code Playgroud)
同
res = (this->*sip_field)();
Run Code Online (Sandbox Code Playgroud)
正确的语法是这样的:
(this->*sip_field)();
Run Code Online (Sandbox Code Playgroud)
或者,如果你想使用.而不是->,那么这个:
((*this).*sip_field)();
Run Code Online (Sandbox Code Playgroud)
我更喜欢前一种语法。