• <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>
            posts - 17,  comments - 2,  trackbacks - 0
            Delegate in Standard C++
            Rating: 

            Ben Chun Pong Chan (view profile)
            January 28, 2002

            Environment: ANSI/ISO C++

            Microsoft introduced a new feature called "delegates" in the .NET framework. It is actually a class that holds a list of function pointers. As long as they own the same function signature, the delegate object can hold static, global, or member function pointers. Now I'm going to do the same in a "unmanaged C++" by way of using the "external polymorphism" pattern.


            (continued)

             

            1. Construct the abstract delegate base class

            class Delegate {
            public:
                  virtual void Invoke()=0;
            protected:
                  Delegate(){}
                  virtual ~Delegate(){}
            };

            2. Construct a derive class which accepts a static/global function pointer

            //NonTypeDelegate.h
            #include "Delegate.h"
            
            class NonTypeDelegate : public Delegate
            {
            public:
               void Invoke();
               NonTypeDelegate(void (*pfn)(int),int iParam);
               virtual ~NonTypeDelegate(){}
            private:
               void (*m_pfn)(int);
               int m_iParam;
            };
            
            //NonTypeDelegate.cpp
            #include "NonTypeDelegate.h"
            #include <iostream>
            
            using namespace std;
            
            NonTypeDelegate::NonTypeDelegate(void (*pfn)(int),
                                             int iParam):m_pfn(pfn),
                                             m_iParam(iParam)
            {
            }
            
            void NonTypeDelegate::Invoke()
            {
               cout << "NonTypeDelegate Invoke\r\n";
               m_pfn(m_iParam);
            }

            3. Construct another derive class which accepts a member function pointer

            //TypeDelegate.hpp
            #include "Delegate.h"
            #include <iostream>
            
            using namespace std;
            
            template <typename T>
            
            class TypeDelegate : public Delegate
            {
            public:
               void Invoke();
               TypeDelegate(T &t, void (T::*pfn)(int), int iParam);
               ~TypeDelegate(){}
            
            private:
               T m_t;
               void (T::*m_pfn)(int);
               int m_iParam;
            };
            
            template<typename T>
            TypeDelegate<T>::TypeDelegate(T &t,
                                          void (T::*pfn)(int),
                                          int iParam):m_t(t),
                                          m_pfn(pfn),
                                          m_iParam(iParam)
            {
            }
            
            template<typename T>
            
            void TypeDelegate<T7gt;::Invoke()
            {
               cout << "TypeDelegate Invoke\r\n";
               (m_t.*m_pfn)(m_iParam);
            }

            4. Now glue up all the stuffs

            #include <iostream>
            #include "NonTypeDelegate.h"
            #include "TypeDelegate.hpp"
            #include <vector>
            
            using namespace std;
            
            void Test(int iParam)
            {
               cout << "Test Invoked\r\n";
            }
            
            class A
            {
             public:
                void Test(int iParam)
                {
                   cout << "A::Test Invoked\r\n";
                }
            };
            
            int main(int argc, char* argv[])
            {
               NonTypeDelegate nTDelegate(Test,1);
            
               A a;
               TypeDelegate<A> tDelegate(a,A::Test,2);
            
               vector<Delegate*> vecpDelegate;
               vecpDelegate.push_back(&nTDelegate);
               vecpDelegate.push_back(&tDelegate);
            
               for (vector<Delegate*>::const_iterator kItr=vecpDelegate.begin();
                   kItr!=vecpDelegate.end();
                   ++kItr)
               {
                   (*kItr)->Invoke();
               }
            
               return 0;
            }

            5. And the output is

            NonTypeDelegate Invoke
            Test Invoked
            TypeDelegate Invoke
            A::Test Invoked

            Conclusion

            Actually, you can derive a class which can accept different signature of functions pointer. Thanks to the powerful "external polymorphism" pattern.

            References

            Chris Cleeland, Douglas C.Schmidt and Timothy H.Harrison External Polymorphism : An Object Structural Pattern for Transparently Extending C++ Concrete Data Types

            posted on 2008-11-06 23:29 BeyondCN 閱讀(537) 評論(0)  編輯 收藏 引用 所屬分類: C++
            久久精品国产亚洲77777| 久久国产免费直播| 久久人爽人人爽人人片AV| 日本强好片久久久久久AAA | 亚洲国产精品久久久久网站| 久久精品中文字幕有码| 久久人人爽人人爽人人片AV东京热 | 天天影视色香欲综合久久| 久久久久人妻精品一区二区三区| 99久久国产综合精品网成人影院| 伊人久久大香线蕉综合影院首页| 777米奇久久最新地址| 久久久亚洲裙底偷窥综合| 99久久免费只有精品国产| 亚洲中文字幕无码久久综合网| 久久精品国产99国产精品| 久久久久亚洲Av无码专| 国产精品久久久久a影院| 国内精品久久久久久中文字幕| 国产精品女同久久久久电影院| 亚洲国产天堂久久综合| 久久国产美女免费观看精品| 国产一区二区三区久久| 青草国产精品久久久久久| 尹人香蕉久久99天天拍| 久久久久亚洲精品无码网址 | 狠狠色丁香婷综合久久| 一本一道久久综合狠狠老 | 国产综合久久久久| 久久久无码精品亚洲日韩按摩| 性欧美大战久久久久久久| 欧美久久一级内射wwwwww.| 久久精品国产一区二区三区| 99久久国产免费福利| 精品久久人人爽天天玩人人妻| 91亚洲国产成人久久精品网址| 亚洲狠狠综合久久| 久久久久99精品成人片三人毛片| 久久乐国产精品亚洲综合| 欧洲国产伦久久久久久久| 久久久久亚洲AV无码专区首JN|