[英]function prototype using in function without pointer
我的講師提到在其他函數中使用函數作為參數。 (我不是指使用指針。是否可能?我在下面顯示)我不明白他做了什么。 誰能用例子解釋一下? 謝謝大家的贊賞。
使用風格是:
int test(double abc(double)){
// bla bla
}
功能是:
double abc(double n){
// function main
}
這些例子是我寫的,我不太確定他們是對的。
這個函數聲明:
int test(double abc(double))
{
// bla bla
}
相當於:
int test(double (*abc)(double))
{
// bla bla
}
abc
參數是函數指針類型的參數( double (*)(double))
)。
對於C標准參考:
(C99,6.7.5.3p8)“參數聲明為”函數返回類型“應調整為”指向函數返回類型的指針“,如6.3.2.1所述。
如果使用指針,可以稍后在函數測試中調用該函數。
typedef double (*func_type)(double);
int test(func_type func) {
// bla bla
cout << func(3);
}
// 2 call
test(double_func)
如果要在調用test之前調用該函數,則只需定義:
int test(double) {
// bla bla
cout << double;
}
// 2 call
test(double_fun(2.0));
正確的選擇取決於你的意圖
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.