再議回呼函數

來源:互聯網
上載者:User
一、回呼函數的概念
    回呼函數是一個程式員不能顯式調用的函數;通過將回呼函數的地址傳給調用者從而實現調用。

二兩個demo

----------------------------------------------------------------------------------------------------------------
void caller(void(*ptr)())
{
ptr(); /* 調用ptr指向的函數 */
}

void func();

int main()
{
p = func;
caller(p);    /* 傳遞函數地址到caller ,caller調用指標p指向的函數*/
}

 如果賦了不同的值給p(不同函數地址),那麼調用者將調用不同地址的函數。賦值可以發生在運行時,這樣使你能實現動態綁定。
-------------------------------------------------------------------------------------------------------------

typedef int(*CallBack)(char *p) ; // 聲明CallBack 類型的函數指標

int A(char *p); // 回呼函數

B(CallBack lpCall,char *pProvide)
{
 ........... // B 的自己實現功能語句
 lpCall(PpProvide); // 藉助回調完成的功能 ,也就是A函數來處理的。
 ........... // B 的自己實現功能語句
}
// -------------- 使用例子 -------------
main()
{
char *p = "hello!";
B(A, p);
}

參考:

http://www.vckbase.com/document/viewdoc/?id=195
http://gethelp.devx.com/techtips/cpp_pro/10min/10min0300.asp
http://tech.ccidnet.com/art/3839/20060314/476731_1.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.