• <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++ 基礎(chǔ)} {C++ 高級(jí)} {C#界面,C++核心算法} {設(shè)計(jì)模式} {C#基礎(chǔ)}

            函數(shù)指針實(shí)例

            需要注意:
            1)函數(shù)指針?biāo)赶虻暮瘮?shù),必須為全局函數(shù)或類的靜態(tài)函數(shù)。


            代碼:
            #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 . . .

            成員函數(shù)指針:
            #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
            }
             

            總結(jié):
            1)一般的全局的函數(shù)指針,只可以使用靜態(tài),全局的函數(shù)。
            2)類的成員函數(shù)指針,可以使用類的成員函數(shù)。可以使用類的public成員函數(shù)指針。

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

            評(píng)論

            # re: 函數(shù)指針實(shí)例 2008-05-23 12:33 夢(mèng)在天涯

            成員函數(shù)指針:
            #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;
            }
              回復(fù)  更多評(píng)論   

            # re: 函數(shù)指針實(shí)例 2008-05-23 14:14 夢(mèng)在天涯

            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;
            }
              回復(fù)  更多評(píng)論   

            公告

            EMail:itech001#126.com

            導(dǎo)航

            統(tǒng)計(jì)

            • 隨筆 - 461
            • 文章 - 4
            • 評(píng)論 - 746
            • 引用 - 0

            常用鏈接

            隨筆分類

            隨筆檔案

            收藏夾

            Blogs

            c#(csharp)

            C++(cpp)

            Enlish

            Forums(bbs)

            My self

            Often go

            Useful Webs

            Xml/Uml/html

            搜索

            •  

            積分與排名

            • 積分 - 1804752
            • 排名 - 5

            最新評(píng)論

            閱讀排行榜

            少妇久久久久久被弄到高潮| 久久综合色老色| 77777亚洲午夜久久多人| 久久久精品久久久久久| 久久精品国产影库免费看| 久久精品人人做人人爽电影蜜月| 久久久噜噜噜久久中文字幕色伊伊| 青青青国产精品国产精品久久久久| 国产精品久久久久9999| 东京热TOKYO综合久久精品| WWW婷婷AV久久久影片| 久久精品国产亚洲AV麻豆网站| 久久综合给久久狠狠97色| 色诱久久久久综合网ywww | 久久精品国产一区| 91精品国产乱码久久久久久| 久久精品国产亚洲AV嫖农村妇女| 91精品国产9l久久久久| 久久精品男人影院| 久久国产精品波多野结衣AV| 久久影院亚洲一区| 97精品依人久久久大香线蕉97| 无码日韩人妻精品久久蜜桃 | 丰满少妇高潮惨叫久久久| 狠狠色丁香久久婷婷综合五月| 韩国三级大全久久网站| 国内精品免费久久影院| 亚洲天堂久久久| 大伊人青草狠狠久久| 久久黄视频| 久久精品水蜜桃av综合天堂 | 一本久久综合亚洲鲁鲁五月天亚洲欧美一区二区 | 国产精品美女久久福利网站| 国产亚洲精品久久久久秋霞| 国产精品久久久久久福利69堂| 国产精品99久久久久久董美香| 伊人久久大香线蕉AV一区二区| 久久精品国产清高在天天线| 国产成人综合久久久久久| 精品久久人人爽天天玩人人妻| 久久久精品一区二区三区|