cod*_*gan 6 c++ gcc function-pointers
为什么会,
#include <iostream>
using namespace std;
int afunction () {return 0;};
int anotherfunction () {return 0;};
int main ()
{
cout << &afunction << endl;
}
Run Code Online (Sandbox Code Playgroud)
给这个,
1
Den*_*ose 15
函数地址不是"true".接受任意函数指针的ostream没有重载.但是有一个布尔值,函数指针可以隐式转换为bool.因此编译器会将afunction其实际值转换为true或false.由于您无法在地址处使用函数0,因此打印的值始终为true,cout显示为1.
这说明了为什么隐式转换通常不受欢迎.如果转换bool是显式的,那么你会遇到编译错误,而不是默默地做错事.