• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            C++ Programmer's Cookbook

            {C++ 基礎} {C++ 高級} {C#界面,C++核心算法} {設計模式} {C#基礎}

            函數指針實例

            需要注意:
            1)函數指針所指向的函數,必須為全局函數或類的靜態函數。


            代碼:
            #include "stdafx.h"
            #include 
            <iostream>

            class A
            {
                typedef 
            int (*fun)(void);

            public:    
                 
            void  Test(fun fun1)
                 
            {
                     (
            *fun1)();
                 }
            ;

                
            static int add(void)
                
            {
                    std::cout
            <<"A::add()"<<std::endl;
                    
            return 2;        
                }

                
            }
            ;

            int add2(void)
            {
                std::cout
            <<"add2()"<<std::endl;
                
            return 2;
                
            }

            static int add3(void)
             
            {
                 std::cout
            <<"add3()"<<std::endl;
                 
            return 3;
             }



            int _tmain(int argc, _TCHAR* argv[])
            {
                A a;    
                a.Test(A::add);
                a.Test(add2);
                a.Test(add3);
                
            return 0;
            }

            //output
            //A::add()
            //add2()
            //add3()
            //Press any key to continue . . .

            成員函數指針:
            #include "stdafx.h" 

            class CMemFuncPtr; 

            typedef 
            int (CMemFuncPtr::*MemFuncPtr)(intint); 

            class CMemFuncPtr 

            public
            int Add(int iFirst, int iSecond) 

            return iFirst + iSecond; 
            }
             
            }


            int _tmain(int argc, _TCHAR* argv[]) 

            MemFuncPtr pfnMemFunc 
            = &CMemFuncPtr::Add; 
            CMemFuncPtr test; 
            (test.
            *pfnMemFunc)(13); 

            return 0
            }
             

            other samples:
            #include "stdafx.h" 

            class Object; 
            typedef 
            int (Object::*MemFuncPtr)(intint); 
            typedef 
            int (*StaticMemFuncPtr)(intint); 

            class Object 

            public
            // non-static member 
            int Add(int iFirst, int iSecond) 

            return iFirst + iSecond; 
            }
             
            // static member 
            static int Sub(int iFirst, int iSecond) 

            return iFirst - iSecond; 
            }
             

            // 
            typedef int (Object::*InClassMemFuncPtr)(intint); 
            typedef 
            int (*InClassStaticMemFuncPtr)(intint); 
            }


            int _tmain(int argc, _TCHAR* argv[]) 

            // Test non-static Add 
            // 
            MemFuncPtr pfnMemFunc = &Object::Add; 
            // Need to bind the member function to a instance (need a this pointer) 
            Object test; 
            int res = (test.*pfnMemFunc)(13); 

            // function call missing argument list; use '&Object::Add' to create a pointer to member 
            //MemFuncPtr pfnMemFunc1 = test.Add; 
            //res = (test.*pfnMemFunc1)(1, 3); 

            Object::InClassMemFuncPtr pfnMemFunc2 
            = &Object::Add; 
            res 
            = (test.*pfnMemFunc2)(13); 

            // Test Static Sub 
            // 
            // cannot convert from 'int (__cdecl *)(int,int)' to 'MemFuncPtr' 
            // Object::Sub Calling convention is __cdecl, but Object::*MemFuncPtr should be thisCall 
            // function signature include calling convention, parameter list and return value.. 

            //pfnMemFunc = Object::Sub; 
            //res = (test.*pfnMemFunc)(1, 3); 

            StaticMemFuncPtr pfnStaticMemFunc 
            = Object::Sub; 
            res 
            = (*pfnStaticMemFunc)(13); 

            StaticMemFuncPtr pfnStaticMemFunc1 
            = &Object::Sub; 
            res 
            = (*pfnStaticMemFunc1)(13); 

            Object::InClassStaticMemFuncPtr pfnStaticMemFunc2 
            = &Object::Sub; 
            res 
            = (*pfnStaticMemFunc2)(13); 


            // stl ? how to use this “function pointer”??? 
            //std::mem_fun<int, Object>(&Object::Add); 
            //std::mem_fun<int, Object>(&Object::Sub); 

            return 0
            }
             

            總結:
            1)一般的全局的函數指針,只可以使用靜態,全局的函數。
            2)類的成員函數指針,可以使用類的成員函數。可以使用類的public成員函數指針。

            posted on 2007-04-20 10:36 夢在天涯 閱讀(3909) 評論(2)  編輯 收藏 引用 所屬分類: CPlusPlus

            評論

            # re: 函數指針實例 2008-05-23 12:33 夢在天涯

            成員函數指針:
            #include "stdafx.h"

            class CMemFuncPtr;

            typedef int (CMemFuncPtr::*MemFuncPtr)(int, int);

            class CMemFuncPtr
            {
            public:
            int Add(int iFirst, int iSecond)
            {
            return iFirst + iSecond;
            }
            };

            int _tmain(int argc, _TCHAR* argv[])
            {
            MemFuncPtr pfnMemFunc = &CMemFuncPtr::Add;
            CMemFuncPtr test;
            (test.*pfnMemFunc)(1, 3);

            return 0;
            }
              回復  更多評論   

            # re: 函數指針實例 2008-05-23 14:14 夢在天涯

            another samples:

            #include "stdafx.h"

            class Object;
            typedef int (Object::*MemFuncPtr)(int, int);
            typedef int (*StaticMemFuncPtr)(int, int);

            class Object
            {
            public:
            // non-static member
            int Add(int iFirst, int iSecond)
            {
            return iFirst + iSecond;
            }
            // static member
            static int Sub(int iFirst, int iSecond)
            {
            return iFirst - iSecond;
            }

            //
            typedef int (Object::*InClassMemFuncPtr)(int, int);
            typedef int (*InClassStaticMemFuncPtr)(int, int);
            };

            int _tmain(int argc, _TCHAR* argv[])
            {
            // Test non-static Add
            //
            MemFuncPtr pfnMemFunc = &Object::Add;
            // Need to bind the member function to a instance (need a this pointer)
            Object test;
            int res = (test.*pfnMemFunc)(1, 3);

            // function call missing argument list; use '&Object::Add' to create a pointer to member
            //MemFuncPtr pfnMemFunc1 = test.Add;
            //res = (test.*pfnMemFunc1)(1, 3);

            Object::InClassMemFuncPtr pfnMemFunc2 = &Object::Add;
            res = (test.*pfnMemFunc2)(1, 3);

            // Test Static Sub
            //
            // cannot convert from 'int (__cdecl *)(int,int)' to 'MemFuncPtr'
            // Object::Sub Calling convention is __cdecl, but Object::*MemFuncPtr should be thisCall
            // function signature include calling convention, parameter list and return value..

            //pfnMemFunc = Object::Sub;
            //res = (test.*pfnMemFunc)(1, 3);

            StaticMemFuncPtr pfnStaticMemFunc = Object::Sub;
            res = (*pfnStaticMemFunc)(1, 3);

            StaticMemFuncPtr pfnStaticMemFunc1 = &Object::Sub;
            res = (*pfnStaticMemFunc1)(1, 3);

            Object::InClassStaticMemFuncPtr pfnStaticMemFunc2 = &Object::Sub;
            res = (*pfnStaticMemFunc2)(1, 3);


            // stl ? how to use this “function pointer”???
            //std::mem_fun<int, Object>(&Object::Add);
            //std::mem_fun<int, Object>(&Object::Sub);

            return 0;
            }
              回復  更多評論   

            公告

            EMail:itech001#126.com

            導航

            統計

            • 隨筆 - 461
            • 文章 - 4
            • 評論 - 746
            • 引用 - 0

            常用鏈接

            隨筆分類

            隨筆檔案

            收藏夾

            Blogs

            c#(csharp)

            C++(cpp)

            Enlish

            Forums(bbs)

            My self

            Often go

            Useful Webs

            Xml/Uml/html

            搜索

            •  

            積分與排名

            • 積分 - 1804434
            • 排名 - 5

            最新評論

            閱讀排行榜

            亚洲伊人久久成综合人影院| 亚洲国产精品无码久久一区二区 | 99久久免费国产精品特黄| 久久精品午夜一区二区福利| 国产精品久久久久久久人人看| 亚洲国产香蕉人人爽成AV片久久| 国产成人精品久久一区二区三区av| 久久国产精品久久| 久久无码av三级| 精品久久久久久国产牛牛app| 色综合久久综合网观看| 亚洲午夜精品久久久久久人妖| 99久久亚洲综合精品成人| 久久精品国产秦先生| 精品久久久久久久久久久久久久久| 国产精品一区二区久久精品无码 | 久久亚洲国产午夜精品理论片| 久久精品这里热有精品| 久久本道综合久久伊人| 亚洲午夜精品久久久久久app| 久久婷婷国产剧情内射白浆| 综合网日日天干夜夜久久| 国产成人久久AV免费| 99久久免费只有精品国产| 四虎影视久久久免费观看| 精品伊人久久大线蕉色首页| 久久精品无码专区免费青青| 中文字幕成人精品久久不卡 | 国产精品一区二区久久精品涩爱| 新狼窝色AV性久久久久久| 狠色狠色狠狠色综合久久| 亚洲国产日韩欧美久久| 99国产精品久久| 久久综合九色综合久99| 久久精品亚洲一区二区三区浴池| 国产高潮国产高潮久久久91 | 久久人人爽人人爽人人片av高请| 久久综合久久久| 99久久无色码中文字幕人妻| 精品水蜜桃久久久久久久| 亚洲第一极品精品无码久久|