4

I am passing an array of C# objects to a managed C++ DLL. The C# class is defined by:

  public class LatLonPointType
    {

        public double Lat;       // Latitude.
        public double Lon;       // Longitude.
    }

I pass an array of LatLonPointTypes to a function in a managed C++ DLL. By trial and error I found that I can pass this to the C++ function using a function argument declared as:

 array <LatLonPointType^> ^PolygonPoints

The following C++ code will extract an element of type LatLonPointType ^.

LatLonPointType ^a = PolygonPoints[0];

My questions are:

  1. I have used C++ a number of years ago, this seems to be similar to the reference declaration &. Is there a good reference to ^?

  2. In the above example, how would I convert a (LatLonPointType ^) to a (LatLonPointType) value?

To clarify:

If I have:

LatLonPointType ^a = PolygonPoints[i];

How would I get a.Lat and a.Lon?

Would I use a->Lat and a->Lon?

Doug Kimzey
  • 1,019
  • 2
  • 17
  • 32
  • 2
    Possible duplicate of [In C++/CLI, what does the hat character ^ do?](http://stackoverflow.com/questions/500580/in-c-cli-what-does-the-hat-character-do) – Arnav Borborah Sep 03 '16 at 15:14
  • 1
    This is not C++. This is Microsoft's corruption of the C++ standard. – Sam Varshavchik Sep 03 '16 at 15:16
  • As for number 2. why would you want to do it? That is like saying to convert a pointer to an object. You, of course, can turn a managed pointer to one that is not managed. – Arnav Borborah Sep 03 '16 at 15:17
  • *The following C++ code will extract an element of type LatLonPointType ^.* -- How can it do that when it really isn't C++ code? – PaulMcKenzie Sep 03 '16 at 15:23
  • Hi Paul and Arnav - I guess my question is: how would I reference the members of LatLonPointType in a LatLonPointType^. For example, if I have a LatLonPointType ^a, how would I obtain a.Lat and a.Lon? – Doug Kimzey Sep 03 '16 at 15:38
  • @DougKimzey I have edited my answer. You have to use the normal `->` operator – Arnav Borborah Sep 03 '16 at 15:55

1 Answers1

0

According to this answer, this hat character, aka ^, creates a managed pointer, that will be garbage collected, not necessarily by you. For example, if you were to create a native object using new, it would look like this:

Object* obj = new Object();

But if you were to use a managed pointer, the syntax would change to:

MObject^ mobj = gcnew MObject();

Currently, I couldn't find how to convert a managed pointer to the object itself, but if you want to convert it to a non managed pointer to the object, see this answer.

EDIT To access members of the class, such as Lon, you can do this:

//Get method
double i = PolygonPoints[n]->Lon;
//Set method
PolygonPoints[n]->Lon = number;
Community
  • 1
  • 1
Arnav Borborah
  • 11,357
  • 8
  • 43
  • 88