为什么不带'&'的赋值在下面的代码中编译?我用 GCC 3.4.6 编译了代码。不带 & 赋值是否正确,或者这是 GCC 的“特性”?
void func() {
}
int main() {
typedef void (*F)();
F f;
f = &func; // the way of assigning pointer to function.
f = func; // this is also working.
(*f)();
return 0;
}
最佳答案
这完全没问题。这两种说法是完全等价的。函数左值通过标准转换转换为指向函数的指针。 §4.3/1:
An lvalue of function type T can be converted to an rvalue of type “pointer to T.” The result is a pointer to the function.
关于c++ - 指向函数赋值的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7245829/