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

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

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

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

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

    int test(int a);?

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

    int test(int a)?
    {?
    ?return a;?
    }
    ******************************************************************************************/
    //函數指針,以typedef 形式定義了一個函數指針類型
    /******************************************************************************************
    #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);//注意,這里不是生命函數指針,而是定義一個函數指針的類型,這個類型是自己定義的,類型名為fp?
    ?fp fpi;//這里利用自己定義的類型名fp定義了一個fpi的函數指針!?
    ?fpi=test;?
    ?cout<<fpi(5)<<"|"<<(*fpi)(10)<<endl;?
    ?return 0;
    }?

    int test(int a)?
    {?
    ?return a;?
    }
    ******************************************************************************************/
    //函數指針作為參數的情形。
    /******************************************************************************************
    #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 函數的內存地址?

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

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

    int test2(int (*ra)(int),int b)//這里定義了一個名字為ra的函數指針?
    {?
    ?int c = ra(10)+b;//在調用之后,ra已經指向fpi所指向的函數地址即test函數?
    ?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()的內存地址和數組a[0]所存儲的地址是否一致"<<t1<<"|"<<a[0]<<endl;?

    ?//cout<<a[0]();//錯誤!指針數組是不能利用數組下標操作調用函數的?

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

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


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

    常用鏈接

    留言簿(21)

    隨筆分類(265)

    隨筆檔案(242)

    相冊

    JAVA網站

    關注的Blog

    搜索

    •  

    積分與排名

    • 積分 - 914507
    • 排名 - 40

    最新評論

    主站蜘蛛池模板: 中文字幕成人免费视频| 亚洲AV无码专区在线亚| 波多野结衣免费视频观看| 99久久免费精品视频| 国产精品内射视频免费| 亚洲AV无码国产剧情| 亚洲天堂2016| 亚洲欧洲国产经精品香蕉网| 亚洲码国产精品高潮在线| www.91亚洲| 四虎影院免费视频| 一个人免费观看在线视频www| 久久久久成人片免费观看蜜芽| 一级美国片免费看| 菠萝菠萝蜜在线免费视频| 亚洲欧美国产欧美色欲| 99热亚洲色精品国产88| 亚洲最大黄色网址| 亚洲综合精品一二三区在线| 国产亚洲精品va在线| 精品亚洲永久免费精品| 亚洲一区爱区精品无码| 亚洲中文字幕久久精品无码APP | 亚洲免费视频网站| 亚洲精品无码成人片久久| 国产gv天堂亚洲国产gv刚刚碰| 亚洲国产成人久久精品99| 亚洲精品97久久中文字幕无码| 免费观看亚洲人成网站| 亚洲AV网站在线观看| 亚洲国产精品专区在线观看 | 国产亚洲精品AAAA片APP| 亚洲av无码专区青青草原| 国产亚洲一卡2卡3卡4卡新区 | 亚洲免费在线视频观看| 精品福利一区二区三区免费视频| 19禁啪啪无遮挡免费网站| 1000部拍拍拍18勿入免费凤凰福利| 四虎影视成人永久免费观看视频| 18级成人毛片免费观看| 福利免费观看午夜体检区|