我正在将一组 C# 对象传递给托管 C++ DLL。 C# 类定义为:
public class LatLonPointType
{
public double Lat; // Latitude.
public double Lon; // Longitude.
}
我将一组 LatLonPointTypes 传递给托管 C++ DLL 中的函数。通过反复试验,我发现我可以使用声明为的函数参数将其传递给 C++ 函数:
array <LatLonPointType^> ^PolygonPoints
以下 C++ 代码将提取类型为 LatLonPointType ^ 的元素。
LatLonPointType ^a = PolygonPoints[0];
我的问题是:
我多年前就用过C++,这好像有点类似于引用声明&。 ^有很好的引用吗?
在上面的示例中,我如何将 (LatLonPointType ^) 转换为 (LatLonPointType) 值?
澄清一下:
如果我有:
LatLonPointType ^a = PolygonPoints[i];
我如何获得 a.Lat 和 a.Lon?
我会使用 a->Lat 和 a->Lon 吗?
最佳答案
根据 this answer ,这个帽子字符,又名 ^
,创建一个托管指针,它将被垃圾收集,不一定由您收集。例如,如果您要使用 new
创建一个 native 对象,它将如下所示:
Object* obj = new Object();
但是如果您要使用托管指针,则语法将更改为:
MObject^ mobj = gcnew MObject();
目前,我找不到如何将托管指针转换为对象本身,但如果您想将其转换为指向对象的非托管指针,请参阅 this answer .
编辑
要访问类的成员,例如 Lon
,您可以这样做:
//Get method
double i = PolygonPoints[n]->Lon;
//Set method
PolygonPoints[n]->Lon = number;
关于c# - C++ 中的数组<T ^> ^T 是什么?如何从数组中提取值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39308401/