Delegate

作者: VinceZeng | 来源:发表于2017-12-06 17:10 被阅读0次

    A delegate is a class that wraps a pointer or reference to an object instance, a member method of that object's class to be called on that object instance, and provides a method to trigger that call.

    Here's an example:

      template <class T>
    class CCallback
    {
    public:
        typedef void (T::*fn)( int anArg );
    
        CCallback(T& trg, fn op)
            : m_rTarget(trg)
            , m_Operation(op)
        {
        }
    
        void Execute( int in )
        {
            (m_rTarget.*m_Operation)( in );
        }
    
    private:
    
        CCallback();
        CCallback( const CCallback& );
    
        T& m_rTarget;
        fn m_Operation;
    
    };
    
    class A
    {
    public:
        virtual void Fn( int i )
        {
        }
    };
    
    
    int main( int /*argc*/, char * /*argv*/ )
    {
        A a;
        CCallback<A> cbk( a, &A::Fn );
        cbk.Execute( 3 );
    }
    
    

    相关文章

      网友评论

          本文标题:Delegate

          本文链接:https://www.haomeiwen.com/subject/jtjlixtx.html