<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    int (*fp)(int a);//這里就定義了一個(gè)指向函數(shù)的指針 。初學(xué)C++ ,以代碼作為學(xué)習(xí)筆記。

    /函數(shù)指針
    /******************************************************************************************
    #include "stdafx.h"
    #include <iostream>?
    #include <string>?
    using namespace std;?

    int test(int a);?

    int _tmain(int argc,_TCHAR* argv[])???
    {?
    ?cout << test << endl;//顯示函數(shù)地址?
    ?int (*fp)(int a);?
    ?fp = test;//將函數(shù)test的地址賦給函數(shù)學(xué)指針fp?
    ?cout << fp(5) << "|" << (*fp)(10) << endl;?
    ?//上面的輸出fp(5),這是標(biāo)準(zhǔn)c++的寫法,(*fp)(10)這是兼容c語言的標(biāo)準(zhǔn)寫法,兩種同意,但注意區(qū)分,避免寫的程序產(chǎn)生移植性問題!?
    ?return 0;
    }?

    int test(int a)?
    {?
    ?return a;?
    }
    ******************************************************************************************/
    //函數(shù)指針,以typedef 形式定義了一個(gè)函數(shù)指針類型
    /******************************************************************************************
    #include "stdafx.h"
    #include <iostream>?
    #include <string>?

    using namespace std;?

    int test(int a);?

    int _tmain(int argc,_TCHAR* argv[])???
    {?
    ?cout<<test<<endl;?
    ?typedef int (*fp)(int a);//注意,這里不是生命函數(shù)指針,而是定義一個(gè)函數(shù)指針的類型,這個(gè)類型是自己定義的,類型名為fp?
    ?fp fpi;//這里利用自己定義的類型名fp定義了一個(gè)fpi的函數(shù)指針!?
    ?fpi=test;?
    ?cout<<fpi(5)<<"|"<<(*fpi)(10)<<endl;?
    ?return 0;
    }?

    int test(int a)?
    {?
    ?return a;?
    }
    ******************************************************************************************/
    //函數(shù)指針作為參數(shù)的情形。
    /******************************************************************************************
    #include "stdafx.h"
    #include <iostream>???
    #include <string>

    using namespace std;???

    int test(int);???

    int test2(int (*ra)(int),int);?

    int _tmain(int argc,_TCHAR* argv[])?????
    {???
    ?cout << test << endl;?
    ?typedef int (*fp)(int);???
    ?fp fpi;?
    ?fpi = test;//fpi賦予test 函數(shù)的內(nèi)存地址?

    ?cout << test2(fpi,1) << endl;//這里調(diào)用test2函數(shù)的時(shí)候,這里把fpi所存儲(chǔ)的函數(shù)地址(test的函數(shù)地址)傳遞了給test2的第一個(gè)形參?
    ?return 0;
    }???

    int test(int a)?
    {???
    ?return a-1;?
    }?

    int test2(int (*ra)(int),int b)//這里定義了一個(gè)名字為ra的函數(shù)指針?
    {?
    ?int c = ra(10)+b;//在調(diào)用之后,ra已經(jīng)指向fpi所指向的函數(shù)地址即test函數(shù)?
    ?return c;?
    }
    ******************************************************************************************/
    #include "stdafx.h"
    #include <iostream>???
    #include <string>???
    using namespace std;?

    void t1(){cout<<"test1";}?
    void t2(){cout<<"test2";}?
    void t3(){cout<<"test3";}?
    int _tmain(int argc,_TCHAR* argv[])?????
    {?
    ?void* a[]={t1,t2,t3};?
    ?cout<<"比較t1()的內(nèi)存地址和數(shù)組a[0]所存儲(chǔ)的地址是否一致"<<t1<<"|"<<a[0]<<endl;?

    ?//cout<<a[0]();//錯(cuò)誤!指針數(shù)組是不能利用數(shù)組下標(biāo)操作調(diào)用函數(shù)的?

    ?typedef void (*fp)();//自定義一個(gè)函數(shù)指針類型?
    ?fp b[]={t1,t2,t3}; //利用自定義類型fp把b[]定義趁一個(gè)指向函數(shù)的指針數(shù)組?
    ?b[0]();//現(xiàn)在利用指向函數(shù)的指針數(shù)組進(jìn)行下標(biāo)操作就可以進(jìn)行函數(shù)的間接調(diào)用了;?
    ?return 0;
    }

    posted on 2008-04-08 23:38 -274°C 閱讀(282) 評(píng)論(1)  編輯  收藏 所屬分類: C++


    FeedBack:
    # re: C++ 之函數(shù)指針
    2008-12-22 10:38 | @
    //typedef int pHelloWorld();
    //pHelloWorld *pHello = (pHelloWorld *)::GetProcAddress(hDll, "HelloWorld");
    typedef int (*pHelloWorld)();
    pHelloWorld pHello = (pHelloWorld)::GetProcAddress(hDll, "HelloWorld");
      回復(fù)  更多評(píng)論
      

    常用鏈接

    留言簿(21)

    隨筆分類(265)

    隨筆檔案(242)

    相冊(cè)

    JAVA網(wǎng)站

    關(guān)注的Blog

    搜索

    •  

    積分與排名

    • 積分 - 914351
    • 排名 - 40

    最新評(píng)論

    主站蜘蛛池模板: 亚洲AV无码欧洲AV无码网站| www.亚洲成在线| 亚洲一区二区三区免费视频| 亚洲日韩国产精品乱-久| 香蕉高清免费永久在线视频| 成人午夜免费视频| 亚洲视频一区在线观看| 最新猫咪www免费人成| 亚洲精品国产日韩无码AV永久免费网| 亚洲伦另类中文字幕| 国产青草视频免费观看97 | 无码免费又爽又高潮喷水的视频| 国产国拍亚洲精品福利| free哆啪啪免费永久| 一级毛片一级毛片免费毛片| 亚洲国产成人资源在线软件| 亚洲日韩国产精品乱| 四虎永久在线观看免费网站网址 | EEUSS影院WWW在线观看免费| 亚洲精品国产福利在线观看| 亚洲av日韩av欧v在线天堂| 亚洲大片免费观看| 男人j进女人p免费视频| 亚洲婷婷综合色高清在线| 亚洲中文字幕无码爆乳av中文| 无码国产精品一区二区免费式影视| 无码的免费不卡毛片视频| 亚洲成a人片在线不卡| 国产av无码专区亚洲av桃花庵 | 亚洲伊人色一综合网| 亚洲无线观看国产精品| 国产精品视_精品国产免费| 5g影院5g天天爽永久免费影院| 九九九国产精品成人免费视频| 亚洲色图激情文学| 久久亚洲日韩精品一区二区三区 | 亚洲人成国产精品无码| 国产成人免费爽爽爽视频 | 亚洲视频免费在线看| a级毛片黄免费a级毛片| 日日摸夜夜添夜夜免费视频 |