繁体   English   中英

如何将这个指针与成员函数的指针一起使用

[英]How can I use this pointer with pointer to member function

我有一个函数指针的typedef:

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

并具有一个返回指向函数的指针的方法:

const auto connect_method = map_algorithm_to_method(*network)

所以我想这样称呼:

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

但出现错误:

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

但是当我这样称呼时:

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

一切都在建造...

如何在不创建对象的情况下调用该方法,因为我已经有一个对象(此指针)

该错误消息听起来像是您在const成员函数内调用非const成员函数指针一样。 this是const成员函数内部的const WlanApiWrapper *因此the object (*this) has type qualifiers (const) that are not compatible with the (non-const) member function

为了解决这个问题,您可以使connect_method常量或使成员函数包含(this->*connect_method)(*network, ssid, pass); 非const。

这样称呼它:

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

这应该适用于所有编译器。

有关更多信息,请阅读如何使用指向成员函数的指针调用成员函数时如何避免语法错误? 在C ++常见问题中

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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