function pointer to a delegate

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • vcquestions

    function pointer to a delegate

    Hi.

    Is there way to have a function pointer to a delegate in c++/cli that
    would allow me to pass delegates with the same signatures as
    parameters to a method?

    I'm working with managed code. Let's say we have 2 delegates:
    public delegate void FirstDelegate( int i );
    public delegate void SecondDelegate( int i );

    classA::method1 ( )
    {
    int i = 1;
    doSomething( );
    m_obj->FirstDelegat e( i );
    }

    class A::method2( )
    {
    doSomething( );
    int j = 2;
    m_obj->SecondDelegate ( j );
    }

    Is there a way to have a function pointer to a delegate so that I can
    just have a common function that would take the delegate as a
    parameter:

    class A::methodInstea dOf1And2( delegateFunctio nPointer )
    {
    int i = 3;
    m_obj->delegateFuncti onPointer( i );
    }

    thanks,
    vcq
  • Paavo Helde

    #2
    Re: function pointer to a delegate

    vcquestions <vcquestions@gm ail.comkirjutas :
    Hi.
    >
    Is there way to have a function pointer to a delegate in c++/cli that
    would allow me to pass delegates with the same signatures as
    parameters to a method?
    >
    I'm working with managed code. Let's say we have 2 delegates:
    In C++ there are no delegates (nor managed code). You will probably get
    better answers by posting in some MS newsgroups or forums.

    In C++, you can form a pointer to a method in a class, with given
    signature, which can resolve to different actual methods when actually
    called. I am quite sure this is not helpful for you so I don't give an
    example.

    Regards
    Paavo


    Comment

    • Paavo Helde

      #3
      Re: function pointer to a delegate

      Paavo Helde <nobody@ebi.eek irjutas:
      vcquestions <vcquestions@gm ail.comkirjutas :
      >
      >Hi.
      >>
      >Is there way to have a function pointer to a delegate in c++/cli that
      >would allow me to pass delegates with the same signatures as
      >parameters to a method?
      >>
      >I'm working with managed code. Let's say we have 2 delegates:
      >
      In C++ there are no delegates (nor managed code). You will probably get
      better answers by posting in some MS newsgroups or forums.
      Oops sorry, mixed up the newsgroup (again!). We are in a mircosoft ng, so
      delegates are presumably on-topic. Too bad I don't know anything about them
      :-(

      Paavo

      Comment

      • Ben Voigt [C++ MVP]

        #4
        Re: function pointer to a delegate

        vcquestions wrote:
        Hi.
        >
        Is there way to have a function pointer to a delegate in c++/cli that
        would allow me to pass delegates with the same signatures as
        parameters to a method?
        >
        I'm working with managed code. Let's say we have 2 delegates:
        public delegate void FirstDelegate( int i );
        public delegate void SecondDelegate( int i );
        That's two delegate types with the same signature.
        >
        classA::method1 ( )
        {
        int i = 1;
        doSomething( );
        m_obj->FirstDelegat e( i );
        That won't work, you can't use the -operator with a type name.

        Maybe if you are a little more careful to distinguish between delegate
        types, methods (aka member functions), and delegate instances, we'll be able
        to answer your question.
        }
        >
        class A::method2( )
        {
        doSomething( );
        int j = 2;
        m_obj->SecondDelegate ( j );
        }
        >
        Is there a way to have a function pointer to a delegate so that I can
        just have a common function that would take the delegate as a
        parameter:
        >
        class A::methodInstea dOf1And2( delegateFunctio nPointer )
        {
        int i = 3;
        m_obj->delegateFuncti onPointer( i );
        }
        >
        thanks,
        vcq

        Comment

        • vcquestions

          #5
          Re: function pointer to a delegate - corrected: passing delegate as aparameter

          Ben, thanks for replying. I was trying to figure out a way to pass a
          delegate as a parameter so that I could pass delegates with the same
          signatures to a method that handles common code. This is straight
          forward ( and does not require function pointers which I thought I'd
          use originally ). When I run code below, I see that DynamicInvoke
          walks the chain of delegates while Method->Invoke just invokes one
          method.

          I suspect that implementation of DynamicInvoke just walks the chain
          and calls Method->Invoke for each. Is that a correct assumption? If
          DynamicInvoke does not incur any additional cost, it's probably the
          way to go.

          Thanks!
          vcq

          public delegate void FirstDelegate( int i );
          public delegate void SecondDelegate( int i );


          void CommonMethod(Sy stem::Delegate^ test)
          {
          //!CommonCode goes here

          //argumest to the invoked method (should be
          passed in as well)
          array<Object^>^ pArgs = gcnew array<Object^>( 1);
          pArgs[0] = 3;

          //dynamic invoke
          test->DynamicInvok e( pArgs );

          //method->invoke
          test->Method->Invoke( test->Target, pArgs );

          //walk the chain of delegates - same as DynamicInvoke ?
          array<Delegate^ >^ tests = test->GetInvocationL ist( );
          for ( int i = 0; i < tests->Length; ++i )
          {
          tests[i]->Method->Invoke( test->Target, pArgs );
          }
          }


          Comment

          • Ben Voigt [C++ MVP]

            #6
            Re: function pointer to a delegate - corrected: passing delegate as a parameter

            vcquestions wrote:
            Ben, thanks for replying. I was trying to figure out a way to pass a
            delegate as a parameter so that I could pass delegates with the same
            signatures to a method that handles common code. This is straight
            forward ( and does not require function pointers which I thought I'd
            use originally ). When I run code below, I see that DynamicInvoke
            walks the chain of delegates while Method->Invoke just invokes one
            method.
            >
            I suspect that implementation of DynamicInvoke just walks the chain
            and calls Method->Invoke for each. Is that a correct assumption? If
            DynamicInvoke does not incur any additional cost, it's probably the
            way to go.
            Yes, but... dynamic invocation is much more expensive.

            The intended way to invoke a delegate is just

            dl->Invoke(the parameters are typesafe here);

            or even simpler,

            dl(the parameters go here);
            >
            Thanks!
            vcq
            >
            public delegate void FirstDelegate( int i );
            public delegate void SecondDelegate( int i );
            >
            >
            void CommonMethod(Sy stem::Delegate^ test)
            {
            //!CommonCode goes here
            >
            //argumest to the invoked method (should be
            passed in as well)
            array<Object^>^ pArgs = gcnew array<Object^>( 1);
            pArgs[0] = 3;
            >
            //dynamic invoke
            test->DynamicInvok e( pArgs );
            >
            //method->invoke
            test->Method->Invoke( test->Target, pArgs );
            >
            //walk the chain of delegates - same as DynamicInvoke ?
            array<Delegate^ >^ tests = test->GetInvocationL ist( );
            for ( int i = 0; i < tests->Length; ++i )
            {
            tests[i]->Method->Invoke( test->Target, pArgs );
            }
            }

            Comment

            • vcquestions

              #7
              Re: function pointer to a delegate - corrected: passing delegate as aparameter

              in my example ( previous post ), where I pass delegates to CommonMethod
              ( System::Delegat e^ test), I can't just
              call test->Invoke( params ) or test( params ) since compiler has no
              knowledge of how to resolve this call ( any delegate could be passed )
              so I have to pay price for late binding. ( Invoke is not even exposed
              on the System::Delegat e ). Please correct me if I'm wrong here.

              That's why I was trying to figure out if there is a better way to
              handle this situation ( have common code that invokes delegates with
              the same signature - e.g., I can pass the whole object on which I
              invoke a delegate and some param that I would switch on and do: obj-
              >Del1( ); or obj->Del2( ) ), but the switch statement does not really
              help in making code clean. I guess there is no magic - the above 2
              choices is all I have...

              Thanks!


              On Nov 17, 8:21 am, "Ben Voigt [C++ MVP]" <r...@nospam.no spamwrote:
              vcquestions wrote:
              Ben, thanks for replying.  I was trying to figure out a way to pass a
              delegate as a parameter so that I could pass delegates with the same
              signatures to a method that handles common code.  This is straight
              forward ( and does not require function pointers which I thought I'd
              use originally ).  When I run code below, I see that DynamicInvoke
              walks the chain of delegates while Method->Invoke just invokes one
              method.
              >
              I suspect that implementation of DynamicInvoke just walks the chain
              and calls Method->Invoke for each.  Is that a correct assumption?  If
              DynamicInvoke does not incur any additional cost, it's probably the
              way to go.
              >
              Yes, but... dynamic invocation is much more expensive.
              >
              The intended way to invoke a delegate is just
              >
              dl->Invoke(the parameters are typesafe here);
              >
              or even simpler,
              >
              dl(the parameters go here);
              >
              >
              >
              >
              >
              Thanks!
              vcq
              >
                     public delegate void FirstDelegate( int i );
                     public delegate void SecondDelegate( int i );
              >
              void CommonMethod(Sy stem::Delegate^ test)
              {
              //!CommonCode goes here
              >
                                        //argumest to the invoked method (should be
              passed in as well)
              array<Object^>^ pArgs = gcnew array<Object^>( 1);
              pArgs[0] = 3;
              >
              //dynamic invoke
              test->DynamicInvok e( pArgs );
              >
              //method->invoke
              test->Method->Invoke( test->Target, pArgs );
              >
              //walk the chain of delegates - same as DynamicInvoke ?
              array<Delegate^ >^ tests = test->GetInvocationL ist( );
              for ( int i = 0; i < tests->Length; ++i )
              {
              tests[i]->Method->Invoke( test->Target, pArgs );
              }
              }- Hide quoted text -
              >
              - Show quoted text -

              Comment

              • Ben Voigt [C++ MVP]

                #8
                Re: function pointer to a delegate - corrected: passing delegate as a parameter

                vcquestions wrote:
                in my example ( previous post ), where I pass delegates to
                CommonMethod ( System::Delegat e^ test), I can't just
                call test->Invoke( params ) or test( params ) since compiler has no
                knowledge of how to resolve this call ( any delegate could be passed )
                so I have to pay price for late binding. ( Invoke is not even exposed
                on the System::Delegat e ). Please correct me if I'm wrong here.
                >
                That's why I was trying to figure out if there is a better way to
                handle this situation ( have common code that invokes delegates with
                the same signature - e.g., I can pass the whole object on which I
                invoke a delegate and some param that I would switch on and do: obj-
                >Del1( ); or obj->Del2( ) ), but the switch statement does not really
                help in making code clean. I guess there is no magic - the above 2
                choices is all I have...
                But the functions all have the same signature, you why are you using
                System::Delegat e? Use an exact delegate type and then early binding will
                work.
                >
                Thanks!
                >
                >
                On Nov 17, 8:21 am, "Ben Voigt [C++ MVP]" <r...@nospam.no spamwrote:
                >vcquestions wrote:
                >>Ben, thanks for replying. I was trying to figure out a way to pass a
                >>delegate as a parameter so that I could pass delegates with the same
                >>signatures to a method that handles common code. This is straight
                >>forward ( and does not require function pointers which I thought I'd
                >>use originally ). When I run code below, I see that DynamicInvoke
                >>walks the chain of delegates while Method->Invoke just invokes one
                >>method.
                >>
                >>I suspect that implementation of DynamicInvoke just walks the chain
                >>and calls Method->Invoke for each. Is that a correct assumption? If
                >>DynamicInvo ke does not incur any additional cost, it's probably the
                >>way to go.
                >>
                >Yes, but... dynamic invocation is much more expensive.
                >>
                >The intended way to invoke a delegate is just
                >>
                >dl->Invoke(the parameters are typesafe here);
                >>
                >or even simpler,
                >>
                >dl(the parameters go here);
                >>
                >>
                >>
                >>
                >>
                >>Thanks!
                >>vcq
                >>
                >>public delegate void FirstDelegate( int i );
                >>public delegate void SecondDelegate( int i );
                >>
                >>void CommonMethod(Sy stem::Delegate^ test)
                >>{
                >>//!CommonCode goes here
                >>
                >>//argumest to the invoked method (should be
                >>passed in as well)
                >>array<Object^ >^ pArgs = gcnew array<Object^>( 1);
                >>pArgs[0] = 3;
                >>
                >>//dynamic invoke
                >>test->DynamicInvok e( pArgs );
                >>
                >>//method->invoke
                >>test->Method->Invoke( test->Target, pArgs );
                >>
                >>//walk the chain of delegates - same as DynamicInvoke ?
                >>array<Delegat e^>^ tests = test->GetInvocationL ist( );
                >>for ( int i = 0; i < tests->Length; ++i )
                >>{
                >>tests[i]->Method->Invoke( test->Target, pArgs );
                >>}
                >>}- Hide quoted text -
                >>
                >- Show quoted text -

                Comment

                • vcquestions

                  #9
                  Re: function pointer to a delegate - corrected: passing delegate as aparameter

                  I posted the answer yesterday - not sure where it is ( maybe I did a
                  reply to author )...

                  We do have delegates with the same signature:
                  public delegate void OnSecondEvent( int i );
                  public delegate void OnCommonEvent( int i );

                  void SameSignatureMe thod( OnCommonEvent^ test)
                  {
                  //!CommonCode goes here
                  test->Invoke( 55 );
                  }

                  I incorrectly used c-style cast to pass an instance of delegate
                  ( which causes an exception ):
                  SameSignatureMe thod( OnCommonEvent^ )( m_pA->SecondHandle r );

                  Using reinterpret_cas t works fine.
                  SameSignatureMe thod( reinterpret_cas t<OnCommonEvent ^>( m_pA-
                  >SecondHandle r ) );
                  Need to review my casting...

                  Thanks Ben!
                  vcq

                  On Nov 17, 6:20 pm, "Ben Voigt [C++ MVP]" <r...@nospam.no spamwrote:
                  vcquestions wrote:
                  in my example ( previous post ), where I pass delegates to
                  CommonMethod ( System::Delegat e^ test), I can't just
                  call test->Invoke( params ) or test( params ) since compiler has no
                  knowledge of how to resolve this call ( any delegate could be passed )
                  so I have to pay price for late binding.  ( Invoke is not even exposed
                  on the System::Delegat e ).  Please correct me if I'm wrong here.
                  >
                  That's why I was trying to figure out if there is a better way to
                  handle this situation ( have common code that invokes delegates with
                  the same signature - e.g., I can pass the whole object on which I
                  invoke a delegate and some param that I would switch on and do: obj-
                  Del1( ); or obj->Del2( ) ), but the switch statement does not really
                  help in making code clean.  I guess there is no magic - the above 2
                  choices is all I have...
                  >
                  But the functions all have the same signature, you why are you using
                  System::Delegat e?  Use an exact delegate type and then early binding will
                  work.
                  >
                  >
                  >
                  >
                  >
                  Thanks!
                  >
                  On Nov 17, 8:21 am, "Ben Voigt [C++ MVP]" <r...@nospam.no spamwrote:
                  vcquestions wrote:
                  >Ben, thanks for replying. I was trying to figure out a way to pass a
                  >delegate as a parameter so that I could pass delegates with the same
                  >signatures to a method that handles common code. This is straight
                  >forward ( and does not require function pointers which I thought I'd
                  >use originally ). When I run code below, I see that DynamicInvoke
                  >walks the chain of delegates while Method->Invoke just invokes one
                  >method.
                  >
                  >I suspect that implementation of DynamicInvoke just walks the chain
                  >and calls Method->Invoke for each. Is that a correct assumption? If
                  >DynamicInvok e does not incur any additional cost, it's probably the
                  >way to go.
                  >
                  Yes, but... dynamic invocation is much more expensive.
                  >
                  The intended way to invoke a delegate is just
                  >
                  dl->Invoke(the parameters are typesafe here);
                  >
                  or even simpler,
                  >
                  dl(the parameters go here);
                  >
                  >Thanks!
                  >vcq
                  >
                  >public delegate void FirstDelegate( int i );
                  >public delegate void SecondDelegate( int i );
                  >
                  >void CommonMethod(Sy stem::Delegate^ test)
                  >{
                  >//!CommonCode goes here
                  >
                  >//argumest to the invoked method (should be
                  >passed in as well)
                  >array<Object^> ^ pArgs = gcnew array<Object^>( 1);
                  >pArgs[0] = 3;
                  >
                  >//dynamic invoke
                  >test->DynamicInvok e( pArgs );
                  >
                  >//method->invoke
                  >test->Method->Invoke( test->Target, pArgs );
                  >
                  >//walk the chain of delegates - same as DynamicInvoke ?
                  >array<Delegate ^>^ tests = test->GetInvocationL ist( );
                  >for ( int i = 0; i < tests->Length; ++i )
                  >{
                  >tests[i]->Method->Invoke( test->Target, pArgs );
                  >}
                  >}- Hide quoted text -
                  >
                  - Show quoted text -- Hide quoted text -
                  >
                  - Show quoted text -

                  Comment

                  • Ben Voigt [C++ MVP]

                    #10
                    Re: function pointer to a delegate - corrected: passing delegate as a parameter



                    "vcquestion s" <vcquestions@gm ail.comwrote in message
                    news:a118bc29-cff6-47eb-a2dc-958dd30778c8@q2 6g2000prq.googl egroups.com...
                    I posted the answer yesterday - not sure where it is ( maybe I did a
                    reply to author )...
                    >
                    We do have delegates with the same signature:
                    public delegate void OnSecondEvent( int i );
                    public delegate void OnCommonEvent( int i );
                    Why? Of course you will have many functions (methods) with the same
                    signature, but why would you need or want more than one delegate type with
                    that signature.

                    In this case, it would be best to simply use the Action<Tdelegat e which
                    "Encapsulat es a method that takes a single parameter and does not return a
                    value." and not create any delegate types at all.

                    Encapsulates a method that has a single parameter and does not return a value.


                    >
                    void SameSignatureMe thod( OnCommonEvent^ test)
                    {
                    //!CommonCode goes here
                    test->Invoke( 55 );
                    }
                    >
                    I incorrectly used c-style cast to pass an instance of delegate
                    ( which causes an exception ):
                    SameSignatureMe thod( OnCommonEvent^ )( m_pA->SecondHandle r );
                    >
                    Using reinterpret_cas t works fine.
                    SameSignatureMe thod( reinterpret_cas t<OnCommonEvent ^>( m_pA-
                    >>SecondHandl er ) );
                    >
                    Need to review my casting...
                    >
                    Thanks Ben!
                    vcq
                    >
                    On Nov 17, 6:20 pm, "Ben Voigt [C++ MVP]" <r...@nospam.no spamwrote:
                    >vcquestions wrote:
                    in my example ( previous post ), where I pass delegates to
                    CommonMethod ( System::Delegat e^ test), I can't just
                    call test->Invoke( params ) or test( params ) since compiler has no
                    knowledge of how to resolve this call ( any delegate could be passed )
                    so I have to pay price for late binding. ( Invoke is not even exposed
                    on the System::Delegat e ). Please correct me if I'm wrong here.
                    >>
                    That's why I was trying to figure out if there is a better way to
                    handle this situation ( have common code that invokes delegates with
                    the same signature - e.g., I can pass the whole object on which I
                    invoke a delegate and some param that I would switch on and do: obj-
                    >Del1( ); or obj->Del2( ) ), but the switch statement does not really
                    help in making code clean. I guess there is no magic - the above 2
                    choices is all I have...
                    >>
                    >But the functions all have the same signature, you why are you using
                    >System::Delega te? Use an exact delegate type and then early binding will
                    >work.
                    >>
                    >>
                    >>
                    >>
                    >>
                    Thanks!
                    >>
                    On Nov 17, 8:21 am, "Ben Voigt [C++ MVP]" <r...@nospam.no spamwrote:
                    >vcquestions wrote:
                    >>Ben, thanks for replying. I was trying to figure out a way to pass a
                    >>delegate as a parameter so that I could pass delegates with the same
                    >>signatures to a method that handles common code. This is straight
                    >>forward ( and does not require function pointers which I thought I'd
                    >>use originally ). When I run code below, I see that DynamicInvoke
                    >>walks the chain of delegates while Method->Invoke just invokes one
                    >>method.
                    >>
                    >>I suspect that implementation of DynamicInvoke just walks the chain
                    >>and calls Method->Invoke for each. Is that a correct assumption? If
                    >>DynamicInvo ke does not incur any additional cost, it's probably the
                    >>way to go.
                    >>
                    >Yes, but... dynamic invocation is much more expensive.
                    >>
                    >The intended way to invoke a delegate is just
                    >>
                    >dl->Invoke(the parameters are typesafe here);
                    >>
                    >or even simpler,
                    >>
                    >dl(the parameters go here);
                    >>
                    >>Thanks!
                    >>vcq
                    >>
                    >>public delegate void FirstDelegate( int i );
                    >>public delegate void SecondDelegate( int i );
                    >>
                    >>void CommonMethod(Sy stem::Delegate^ test)
                    >>{
                    >>//!CommonCode goes here
                    >>
                    >>//argumest to the invoked method (should be
                    >>passed in as well)
                    >>array<Object^ >^ pArgs = gcnew array<Object^>( 1);
                    >>pArgs[0] = 3;
                    >>
                    >>//dynamic invoke
                    >>test->DynamicInvok e( pArgs );
                    >>
                    >>//method->invoke
                    >>test->Method->Invoke( test->Target, pArgs );
                    >>
                    >>//walk the chain of delegates - same as DynamicInvoke ?
                    >>array<Delegat e^>^ tests = test->GetInvocationL ist( );
                    >>for ( int i = 0; i < tests->Length; ++i )
                    >>{
                    >>tests[i]->Method->Invoke( test->Target, pArgs );
                    >>}
                    >>}- Hide quoted text -
                    >>
                    >- Show quoted text -- Hide quoted text -
                    >>
                    >- Show quoted text -
                    >

                    Comment

                    • vcquestions

                      #11
                      Re: function pointer to a delegate - corrected: passing delegate as aparameter

                      Why? Of course you will have many functions (methods) with the same
                      signature, but why would you need or want more than one delegate type with
                      that signature.
                      purely out of curiousity :) - originally trying to figure out how to
                      pass delegates as parameters and then trying to figure out why I could
                      not pass 2 delegates with the same signature to the common handling
                      functions. As it turns out ( from this thread ), I can & in a few
                      different ways.
                      In this case, it would be best to simply use the Action<Tdelegat e
                      understood.

                      Thanks!


                      On Nov 18, 4:08 pm, "Ben Voigt [C++ MVP]" <r...@nospam.no spamwrote:
                      "vcquestion s" <vcquesti...@gm ail.comwrote in message
                      >
                      news:a118bc29-cff6-47eb-a2dc-958dd30778c8@q2 6g2000prq.googl egroups.com...
                      >
                      I posted the answer yesterday - not sure where it is ( maybe I did a
                      reply to author )...
                      >
                      We do have delegates with the same signature:
                      public delegate void OnSecondEvent( int i );
                      public delegate void OnCommonEvent( int i );
                      >
                      Why?  Of course you will have many functions (methods) with the same
                      signature, but why would you need or want more than one delegate type with
                      that signature.
                      >
                      In this case, it would be best to simply use the Action<Tdelegat e which
                      "Encapsulat es a method that takes a single parameter and does not return a
                      value." and not create any delegate types at all.
                      >
                      Encapsulates a method that has a single parameter and does not return a value.

                      >
                      >
                      >
                      >
                      >
                      void SameSignatureMe thod( OnCommonEvent^ test)
                      {
                      //!CommonCode goes here
                      test->Invoke( 55 );
                      }
                      >
                      I incorrectly used c-style cast to pass an instance of delegate
                      ( which causes an exception ):
                      SameSignatureMe thod( OnCommonEvent^ )( m_pA->SecondHandle r );
                      >
                      Using reinterpret_cas t works fine.
                      SameSignatureMe thod( reinterpret_cas t<OnCommonEvent ^>( m_pA-
                      >SecondHandle r ) );
                      >
                      Need to review my casting...
                      >
                      Thanks Ben!
                      vcq
                      >
                      On Nov 17, 6:20 pm, "Ben Voigt [C++ MVP]" <r...@nospam.no spamwrote:
                      vcquestions wrote:
                      in my example ( previous post ), where I pass delegates to
                      CommonMethod ( System::Delegat e^ test), I can't just
                      call test->Invoke( params ) or test( params ) since compiler has no
                      knowledge of how to resolve this call ( any delegate could be passed)
                      so I have to pay price for late binding.  ( Invoke is not even exposed
                      on the System::Delegat e ).  Please correct me if I'm wrong here.
                      >
                      That's why I was trying to figure out if there is a better way to
                      handle this situation ( have common code that invokes delegates with
                      the same signature - e.g., I can pass the whole object on which I
                      invoke a delegate and some param that I would switch on and do: obj-
                      Del1( ); or obj->Del2( ) ), but the switch statement does not really
                      help in making code clean.  I guess there is no magic - the above 2
                      choices is all I have...
                      >
                      But the functions all have the same signature, you why are you using
                      System::Delegat e?  Use an exact delegate type and then early bindingwill
                      work.
                      >
                      Thanks!
                      >
                      On Nov 17, 8:21 am, "Ben Voigt [C++ MVP]" <r...@nospam.no spamwrote:
                      vcquestions wrote:
                      >Ben, thanks for replying. I was trying to figure out a way to passa
                      >delegate as a parameter so that I could pass delegates with the same
                      >signatures to a method that handles common code. This is straight
                      >forward ( and does not require function pointers which I thought I'd
                      >use originally ). When I run code below, I see that DynamicInvoke
                      >walks the chain of delegates while Method->Invoke just invokes one
                      >method.
                      >
                      >I suspect that implementation of DynamicInvoke just walks the chain
                      >and calls Method->Invoke for each. Is that a correct assumption? If
                      >DynamicInvok e does not incur any additional cost, it's probably the
                      >way to go.
                      >
                      Yes, but... dynamic invocation is much more expensive.
                      >
                      The intended way to invoke a delegate is just
                      >
                      dl->Invoke(the parameters are typesafe here);
                      >
                      or even simpler,
                      >
                      dl(the parameters go here);
                      >
                      >Thanks!
                      >vcq
                      >
                      >public delegate void FirstDelegate( int i );
                      >public delegate void SecondDelegate( int i );
                      >
                      >void CommonMethod(Sy stem::Delegate^ test)
                      >{
                      >//!CommonCode goes here
                      >
                      >//argumest to the invoked method (should be
                      >passed in as well)
                      >array<Object^> ^ pArgs = gcnew array<Object^>( 1);
                      >pArgs[0] = 3;
                      >
                      >//dynamic invoke
                      >test->DynamicInvok e( pArgs );
                      >
                      >//method->invoke
                      >test->Method->Invoke( test->Target, pArgs );
                      >
                      >//walk the chain of delegates - same as DynamicInvoke ?
                      >array<Delegate ^>^ tests = test->GetInvocationL ist( );
                      >for ( int i = 0; i < tests->Length; ++i )
                      >{
                      >tests[i]->Method->Invoke( test->Target, pArgs );
                      >}
                      >}- Hide quoted text -
                      >
                      - Show quoted text -- Hide quoted text -
                      >
                      - Show quoted text -- Hide quoted text -
                      >
                      - Show quoted text -

                      Comment

                      Working...