我正在Linux上使用gtkmm库为GUI绘制一个简单菜单。
在下面的代码中,编译器抱怨无法解析地址
sigc::mem_fun(*this, AppWindow::hide)));
^
appwindow.cpp:15:41: note: could not resolve address from overloaded function
但是当我插入&
它时可以编译
m_menu_app.items().push_back(MenuElem("Quit",
sigc::mem_fun(*this, &AppWindow::hide)));
这有什么区别?hide
函数不是仅是地址吗?
这是函数到指针转换[conv.func]的确切定义:
函数类型的左值
T
可以转换为“指针指向”的前值T
。结果是指向该函数的指针。55
55) 此转换永远不会应用于非静态成员函数,因为无法获得引用非静态成员函数的左值。
因此,我们在正常的非成员函数1上看到的衰减不适用,您需要显式获取地址。
IE
void f();
struct A {
void f();
static void g();
};
auto a = f; // Ok: auto deduced as void(*)()
auto b = A::f; // Error: A::f not an lvalue, auto cannot be deduced
auto c = A::g; // Ok: auto deduced as void(*)()
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句