0

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)

Andriy
  • 362
  • 3
  • 16
  • 1
    Do you, by any chance, happen to try to do what you're trying to do in a const method? Note that your `connect_func` type is a pointer to a non-const member function… – Michael Kenzel Apr 20 '19 at 00:10
  • When people ask you to post a reproducible example, do so instead of deleting your question. You *can* parse space separated numbers with `long.Parse("3 567",NumberStyles.Number, new NumberFormatInfo{NumberGroupSeparator=" "})`. The separator may not be an actual space though, it may be a non-breaking space or something similar – Panagiotis Kanavos May 20 '19 at 15:06

2 Answers2

1

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.

Indiana Kernick
  • 5,041
  • 2
  • 20
  • 50
  • 1
    For completeness' sake, one might add that either the member function containing the call through the member function pointer has to be made non-const, *or* the member function pointer has to point to a const member function. In light of what the name `connect_func` suggests, the latter is most likely not going to work in the case at hand though… – Michael Kenzel Apr 20 '19 at 01:22
0

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

kert
  • 2,161
  • 21
  • 22
  • 1
    The unary `*` operator has higher precedence than the `.*` operator, so that shouldn't be the issue here. I do agree though that the use of additional parentheses would make the whole thing slightly more pleasant on the eyes… – Michael Kenzel Apr 20 '19 at 00:47
  • 2
    Why not use `this->*connect_method` instead? – Indiana Kernick Apr 20 '19 at 00:47