[英]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.