[英]Printing the address of member function
struct Widget {
void test() {}
};
int func() {}
int main() {
std::cout << &Widget::test << std::endl;
std::cout << Widget::test << std::endl;
std::cout << func << std::endl;
std::cout << &func << std::endl;
}
In this code only the second line of main function doesn't compile. 在这段代码中,只有第二行main函数不能编译。 The others print
1
. 其他人打印
1
。 Why does it print 1
. 为什么打印
1
。 Shouldn't print the address of function? 不应该打印功能的地址? And why second doesn't compile but first does?
为什么第二个不编译,但首先呢?
Why does it print 1. Shouldn't print the address of function?
为什么要打印1.不应该打印功能的地址?
No. std::cout
can print a void*
, but there's no implicit conversion from function pointer types to void*
(for neither regular function pointers nor pointer-to-member types). 编号
std::cout
可以打印一个void*
,但是没有从函数指针类型到void*
隐式转换(对于常规函数指针和指向成员类型的指针都没有)。 There's a conversion from function pointer types to bool
though. 虽然有从函数指针类型到
bool
的转换。 That's what we end up with. 这就是我们最终的结果。
And why second doesn't compile but first does?
为什么第二个不编译,但首先呢?
Because the standard requires you to use &
to get the address of a member function. 因为标准要求您使用
&
来获取成员函数的地址。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.