简体   繁体   中英

How can I use this pointer with pointer to member function

I have typedef for a function pointer:

typedef bool(WlanApiWrapper::* (connect_func) )(WLAN_AVAILABLE_NETWORK, const char *, const char *);

and have a method that returns a pointer to function:

const auto connect_method = map_algorithm_to_method(*network)

So I want to call that like that:

(*this.*connect_method)(*network, ssid, pass);

but gets error:

Error (active)  E0315   the object has type qualifiers that are not compatible with the member function CppWlanHack C:\Projects\CppWlanHack\CppWlanHack\WlanApiWrapper.cpp  52  

but when I call that like that:

WlanApiWrapper f;
(f.*connect_method)(*network, ssid, pass);

all is building...

How can I call the method without creating an object, because I've already had an object (this pointer)

The error message sounds to me like you're calling a non-const member function pointer inside a const member function. this is a const WlanApiWrapper * inside a const member function so the object (*this) has type qualifiers (const) that are not compatible with the (non-const) member function .

To solve this, you can either make the connect_method const or make the member function that contains (this->*connect_method)(*network, ssid, pass); non-const.

Call it like this:

((*this).*connect_method)(*network, ssid, pass);

This should work with all compilers.

For more info, read How can I avoid syntax errors when calling a member function using a pointer-to-member-function? in C++ FAQ

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM