跟进其他两个答案,这是一些实际代码的样子
Poly.cpp (C++/CLI)
#include "stdafx.h"
#include "Poly.h"
#include "findNearestPoly.h"
using namespace System;
namespace CStyleArrays
{
public ref class Poly abstract sealed // "abstract sealed" = static
{
public:
static int FindNearest(Tuple<float, float, float>^ center, Tuple<float, float, float>^ extents,
[Runtime::InteropServices::Out] Tuple<float, float, float>^% nearestPt) {
const float pCenter[] = { center->Item1, center->Item2, center->Item3};
const float pExtents[] = { extents->Item1, extents->Item2, extents->Item3};
float pNearestPt[3];
int retval = findNearestPoly(pCenter, pExtents, nullptr /*filter*/, nullptr /*nearestRef*/, pNearestPt);
// if (retval == success)
{
nearestPt = Tuple::Create(pNearestPt[0], pNearestPt[1], pNearestPt[2]);
}
return retval;
}
static int FindNearest(cli::array<float>^ center, cli::array<float>^ extents, cli::array<float>^% nearestPt) {
if ((center->Length != 3) || (extents->Length != 3) || (nearestPt->Length != 3))
throw gcnew ArgumentOutOfRangeException();
const pin_ptr<float> pinCenter = ¢er[0]; // "... if any element of an array is pinned, then the whole array is also pinned ..."
const float* pCenter = pinCenter;
const pin_ptr<float> pinExtents = &extents[0];
const float* pExtents = pinExtents;
const pin_ptr<float> pinNearestPt = &nearestPt[0];
float* pNearestPt = pinNearestPt;
return findNearestPoly(pCenter, pExtents, nullptr /*filter*/, nullptr /*nearestRef*/, pNearestPt);
}
};
}
示例 C# 将是
namespace TestCStyleArrays
{
class Program
{
static void Main(string[] args)
{
{
var center = Tuple.Create(0f, 1f, 2f);
var extents = Tuple.Create(10f, 20f, 30f);
Tuple<float, float, float> nearestPt;
CStyleArrays.Poly.FindNearest(center, extents, out nearestPt);
}
{
var center = new[] { 0f, 1f, 2f };
var extents = new[] { 10f, 20f, 30f };
var nearestPt = new float[3];
CStyleArrays.Poly.FindNearest(center, extents, ref nearestPt);
}
}
}
}