471,079 Members | 1,106 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,079 software developers and data experts.

Delegate as argument (C++/CLI)

Hi,

I am trying to convert a set of classes written in VC++ 6.0 to a .DLL
file using VC++ .NET (Visual Studio 2005) and C++/CLI.

I made one of the classes (one at the root level) to be a 'ref' class.
This class used pointer to a function. Since CLR does not support a
ref class to have a pointer to a function directly, I changed it to a
'delegate'.

Now the problem is - a function of one of the other classes (and not a
ref class) expects an argument which is supposed to be a pointer to a
function. This argument is returned from a function in the root class.
Now that I have changed the pointer variable to a delegate, the
compiler complains that it cannot convert from one type (delegate,
which the root class function returns) to another (pointer to
function, which the function of the other class expects).

Here is the snippet:

typedef double (* DISTANCE_FUNC) (double x1, double y1, double x2,
double y2);
delegate double delDistanceFunction(double x1, double y1, double x2,
double y2);

public ref class root_class {
delDistanceFunction^ pDistanceFunction;
delDistanceFunction^ getDistanceFunction() {
return pDistanceFunction;
}
}

public class not_a_ref_class {
not_a_ref_class(DISTANCE_FUNC func);
}

This statement used in root_class gives error:

not_a_ref_class narc = new not_a_ref_class(getDistanceFunction());

It will be great if somebody can help me solve this problem.
Thanks in advance for your time,
Vijay.

May 10 '07 #1
3 4214
Hi Vijay,

<vi**********@gmail.comwrote in message
news:11**********************@y5g2000hsa.googlegro ups.com...
Hi,

I am trying to convert a set of classes written in VC++ 6.0 to a .DLL
file using VC++ .NET (Visual Studio 2005) and C++/CLI.

I made one of the classes (one at the root level) to be a 'ref' class.
This class used pointer to a function. Since CLR does not support a
ref class to have a pointer to a function directly, I changed it to a
'delegate'.

Now the problem is - a function of one of the other classes (and not a
ref class) expects an argument which is supposed to be a pointer to a
function. This argument is returned from a function in the root class.
Now that I have changed the pointer variable to a delegate, the
compiler complains that it cannot convert from one type (delegate,
which the root class function returns) to another (pointer to
function, which the function of the other class expects).

Here is the snippet:

typedef double (* DISTANCE_FUNC) (double x1, double y1, double x2,
double y2);
delegate double delDistanceFunction(double x1, double y1, double x2,
double y2);

public ref class root_class {
delDistanceFunction^ pDistanceFunction;
delDistanceFunction^ getDistanceFunction() {
return pDistanceFunction;
}
}

public class not_a_ref_class {
not_a_ref_class(DISTANCE_FUNC func);
}

This statement used in root_class gives error:

not_a_ref_class narc = new not_a_ref_class(getDistanceFunction());

It will be great if somebody can help me solve this problem.
Thanks in advance for your time,
Vijay.
I have written a blog entry about native function pointers. Look at
www.heege.net.

Marcus

May 15 '07 #2
On May 15, 1:19 am, "Marcus Heege" <nos...@heege.netwrote:
Hi Vijay,

<vijay.gan...@gmail.comwrote in message

news:11**********************@y5g2000hsa.googlegro ups.com...


Hi,
I am trying to convert a set of classes written in VC++ 6.0 to a .DLL
file using VC++ .NET (Visual Studio 2005) and C++/CLI.
I made one of the classes (one at the root level) to be a 'ref' class.
This class used pointer to a function. Since CLR does not support a
ref class to have a pointer to a function directly, I changed it to a
'delegate'.
Now the problem is - a function of one of the other classes (and not a
ref class) expects an argument which is supposed to be a pointer to a
function. This argument is returned from a function in the root class.
Now that I have changed the pointer variable to a delegate, the
compiler complains that it cannot convert from one type (delegate,
which the root class function returns) to another (pointer to
function, which the function of the other class expects).
Here is the snippet:
typedef double (* DISTANCE_FUNC) (double x1, double y1, double x2,
double y2);
delegate double delDistanceFunction(double x1, double y1, double x2,
double y2);
public ref class root_class {
delDistanceFunction^ pDistanceFunction;
delDistanceFunction^ getDistanceFunction() {
return pDistanceFunction;
}
}
public class not_a_ref_class {
not_a_ref_class(DISTANCE_FUNC func);
}
This statement used in root_class gives error:
not_a_ref_class narc = new not_a_ref_class(getDistanceFunction());
It will be great if somebody can help me solve this problem.
Thanks in advance for your time,
Vijay.

I have written a blog entry about native function pointers. Look atwww.heege.net.

Marcus- Hide quoted text -

- Show quoted text -
Nice writing on the subject on your blog. Thanks for sharing!
I have a similar situation where I simply convert some legacy C++
(VC6) libs to VC2005 DLL projects with /clr support. The DLL will be
called from managed code. The legacy code uses static variables that
need to be initialized. But I am not sure if the managed code do that
on my behalf when the DLL get loadded or I should explicitly call some
routines to force an initialization. The postings (a lot of them) out
on the Internet are confusing since MS has different ways to
initialize statics in different versions of VCs. There is no clear say
on how to do this for C++/CLI under 2005. Can you shed some lights on
this issue.

Thank!
zz

May 15 '07 #3
Hi Marcus,

Thank you very much for your help. I was able to apply your solution
to my problem. It did save a lot of time and effort of searching
around!!

Thanks!
Vijay.

May 18 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Budi | last post: by
5 posts views Thread by Edward Diener | last post: by
5 posts views Thread by karch | last post: by
1 post views Thread by Kerry Jenkins | last post: by
7 posts views Thread by Andreas Reiff | last post: by
26 posts views Thread by raylopez99 | last post: by
10 posts views Thread by vcquestions | last post: by
reply views Thread by leo001 | last post: by

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.