看板 EE_DSnP 關於我們 聯絡資訊
※ 引述《popo4231 (小泰)》之銘言: : 想問一下function pointer解參考之後是什麼東西 : 我試著將funtion ptr前面放很多個*結果好像並不會error : 而且作用上並沒有問題 : code: : #include<iostream> : using namespace std; : int add(int a,int b) : { : return a+b; : } : int sub(int a,int b) : { : return a-b; : } : int addorsub(int a,int b,int (*operate) (int,int)) // function pointer : { : return operate(a,b); : } : int main() : { : int (*FUNPTR[2])(int,int)={add,sub}; : cout<<(*****FUNPTR[0])(10,5)<<endl; : cout<<(*add)(10,5)<<endl; : cout<<(**add)(10,5)<<endl; // 到底function pointer解參考之後是啥東西 ? : cout<<addorsub(5,3,FUNPTR[0])<<endl; : cout<<addorsub(5,3,FUNPTR[1])<<endl; : system("pause"); : return 0; : } 在 funtion pointer 的使用上, (*add)(10, 5) 與 add(10, 5) 都可以 refer 到 add 這個 funtion (的開始位置), 也就是說 (*add) 與 (add) 其實是一樣的意思, 所以 (不是很正式的証明) --- (**add) = (*(*add)) = (*add) = (add) = add 不過我是覺得這也可能是 compiler or C++ language 的漏洞, 搞不好以後的標準就不允許加那麼多星了... 所以, 正常用(operate(a, b) or (*operate)(a, b) )就好. 至於一般的指標變數, *p != p, 所以當然不會有這種情況了. 另外, 一般變數應該是可以三顆星以上吧? 例如說: int ***p = new int **[3]; p[0] = new int *[10]; // p[0] is an int ** (*p)[0] = new int[5]; // (*p) = p[0], so here (*p)[0] = p[0][0] is an int* (**p)[0] = 8; // (**p) = p[0][0], so here (**p)[0] = p[0][0][0] is an int cout << ***p << endl; // print out '8' 小心不要搞錯參照到第幾顆星了... -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 59.121.134.219