在下面給出的代碼片段,當我寫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
但令我驚訝的是,沒有衰減,我沒有收到上述錯誤。
為什么/怎么會這樣?也就是說,為什么沒有衰變。
uj5u.com熱心網友回復:
為什么 A 不衰減為指向函式的指標?
錯誤訊息說函式 ( int()) 不能隱式轉換為指向函式 ( int (**)())的指標的指標,因為運算式 ( A)的型別是函式。
如果存在通過衰減型別到目標型別的有效轉換序列,則該函式將衰減。但是沒有這樣的轉換序列,所以程式格式不正確。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/405193.html
標籤:
