【发布时间】:2022-01-05 08:22:40
【问题描述】:
在下面给出的代码sn-p中,当我写f = A;时,为什么A不衰减指向一个函数的指针?
//Func is alias for "pointer to a function that returns an int and does not take any parameter"
typedef int (*Func)();
int A(){
return 1;
}
int main()
{
Func* f = &A;//cannot convert ‘int (*)()’ to ‘int (**)()’ in initialization - I UNDERSTAND THIS ERROR
f = A;//error: cannot convert ‘int()’ to ‘int (**)()’ in assignment - My QUESTION IS THAT IN THIS CASE WHY DOESN'T "A" decay to a pointer to a function and give the same error as the above
}
我知道为什么Func *f = &A; 会产生错误。但我预计f = A; 会产生相同的错误,因为我认为在这种情况下A 将衰减为指向函数的指针,因此应该产生与Func*f = &A; 相同的错误。准确地说,我以为我会得到错误
error: cannot convert ‘int(*)()’ to ‘int (**)()’ in assignment
但令我惊讶的是,没有衰减,我没有收到上述错误。
为什么/怎么会这样?也就是为什么没有衰变。
【问题讨论】:
-
知道了。误读了问题。
标签: c++ function c++11 function-pointers