Function pointers in C ++: error: should use '. * 'or' -> * 'to call the function member-pointer in the function

The code for the fragment is as follows. It is impossible to understand why I am getting this error.

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 
+4
source share
3 answers
 (this).*sip_field(); 

There are two problems with this expression:

  • this is a pointer, so you should use ->* to call a member function using a pointer on it.

  • calling the function ( () ) has a higher priority than the pointer operator (either .* , or ->* ), so you need parentheses to group the expression correctly.

The correct expression is:

 (this->*sip_field)(); 
+12
source

It looks like you are calling a pointer to a pointer, but you are calling it with the syntax of a star-point. Try replacing

 res = (this).*sip_field(); 

from

 res = (this->*sip_field)(); 
+9
source

The correct syntax is as follows:

 (this->*sip_field)(); 

Or if you want to use . instead of -> then this:

 ((*this).*sip_field)(); 

I would prefer the previous syntax.

+3
source

Source: https://habr.com/ru/post/1416195/


All Articles