• <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 閱讀(533) 評論(0)  編輯 收藏 引用 所屬分類: C++
            av无码久久久久久不卡网站| 狠狠色综合网站久久久久久久| 欧美国产精品久久高清| 久久嫩草影院免费看夜色| 久久乐国产综合亚洲精品| AAA级久久久精品无码片| 久久久精品视频免费观看| 亚洲成av人片不卡无码久久| 欧美大香线蕉线伊人久久| 国产精品欧美久久久久天天影视| 中文国产成人精品久久亚洲精品AⅤ无码精品 | 久久人人爽人人爽人人片AV麻豆| 色婷婷狠狠久久综合五月| 久久精品国产精品国产精品污| 蜜臀久久99精品久久久久久| 性欧美丰满熟妇XXXX性久久久 | 亚洲国产精品综合久久一线| 精品久久久久久无码中文字幕一区| 久久99精品久久久久久齐齐 | 久久久久国产精品嫩草影院| 久久久国产精品福利免费 | 亚洲国产成人精品女人久久久| 国产V亚洲V天堂无码久久久| 久久精品国产AV一区二区三区| 99国内精品久久久久久久| AAA级久久久精品无码片| 蜜臀久久99精品久久久久久小说| 久久亚洲精品国产亚洲老地址| 国产69精品久久久久99尤物| 99久久综合狠狠综合久久止| 久久婷婷五月综合97色直播| 国产69精品久久久久观看软件| 精品国产91久久久久久久a| 亚洲国产精品一区二区久久| 久久精品国产亚洲欧美| 国产一级持黄大片99久久| 久久精品国产一区二区三区日韩| 国产精品无码久久综合| 国产成年无码久久久久毛片| 精品一区二区久久| 国产成人精品久久|