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. The others print 1
. Why does it print 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?
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). There's a conversion from function pointer types to bool
though. 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.