473,842 Members | 1,585 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Nov 11 '08 #1
10 2136
vcquestions <vc*********@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
Nov 11 '08 #2
Paavo Helde <no****@ebi.eek irjutas:
vcquestions <vc*********@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

Nov 11 '08 #3
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

Nov 12 '08 #4
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 );
}
}
Nov 16 '08 #5
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 );
}
}

Nov 17 '08 #6
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 -
Nov 17 '08 #7
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 -

Nov 18 '08 #8
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 -
Nov 18 '08 #9


"vcquestion s" <vc*********@gm ail.comwrote in message
news:a1******** *************** ***********@q26 g2000prq.google groups.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.

http://msdn.microsoft.com/en-us/library/018hxwa8.aspx

>
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 -
Nov 18 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2120
by: Ole | last post by:
hello, Little problem: struct operatable { char * operatable_id; int ( * delegate ) ( ... ); somedatatype data; };
3
11882
by: David N | last post by:
Hi All, I just wonder if in C#, I can develop a user defined control that can call its parent function which is not yet developed. For example, how do I make my user control call a to-be-developed-function cmdOkay_Click() function as described below. 1. Create an user control that contains an OK button as below Public Class MyButton:System.Windows.Form.UserControl
1
1988
by: edgekaos | last post by:
I want to pass a structure to a unmanaged dll function. Inside this structure there is a pointer to a callback function. Unmanaged code will call this managed function as a callback function. I know how to pass the structure and define a delegate for the callback but I don't know how to define the marshelling for the callback pointer.
4
5147
by: H.B. | last post by:
Hi, I successfully implement a static callback function for my dll usign delegates. Now, I need to use member function instead of static function. How can I make that (in Managed C++). Hugo
6
1244
by: Peter Oliphant | last post by:
Here is a simplification of my code. Basically, I have a class (A) that can be constructed using a function pointer to a function that returns a bool with no parameters. I then want to create an instance of this class (A) in another class (B) which uses one of its own methods of the 'proper form' to initialize the instance. But I get an error: __gc class ClassA { public: ClassA( bool (*func)(void) ) {//---some code---//} // constructor
10
15191
by: ChrisB | last post by:
Coming from a C/C++ background, how would I pass a function pointer to a function? I want to write a function that handles certain thread spawning. Here's what I'm trying to invision: function( thesub as <functionptr?> ) dim t as new system.threading.thread( _ new system.threading.threadstart( Addressof thesub )) .... How can I get something like that going in VB.Net?
2
3411
by: sg71.cherub | last post by:
Here I am meeting a problem. I am transfering unmanaged C/C++ codes to managed C# codes. For the general C/C++ function pointer, it is easy to implement its function by C# delegate. e.g. Declare (C/C++): double (* f) (double * x, void * parms); Declare (C#): public delegate double f (double x, object parms); And the ways to use them are logically similiar.
0
2022
by: Haxan | last post by:
Hi, I have an unmanaged application that converts a function pointer to a delegate and then pass this as a parameter(delegate) to a managed function which then invokes it. Currently Im able to jump to this unmanaged function, but the values of the parameters inside this function Im seeing are not correct(they have some garbage values). //unmanaged class (C++ application)
6
2166
by: jmDesktop | last post by:
In a function that takes another function (function pointer) as a argument, or the callback function, which is the one that "calls back"? I'm having a hard time understanding the language. Am I right that if function A is: function A(*function pointer to B aka callback func, other arguments) { call (B); //calls }
0
9872
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10945
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10672
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10314
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7858
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7038
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5696
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5885
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4499
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.