Question

I'm working on some software that is written in native C++ with a managed C++ wrapper and the user interface in C#. I need to pass some information from the native code all the way up to the interface. The best way I can think to pass my information is in a list of tuples. I understand that in the native C++ I need to use a list<tuple<..>> and that works fine. What I want to do now, is take that output in the wrapper, and return a List<Tuple<..>> which is the System::Collections::Generic::List and System::Tuple instead of the stl ones from the native. I know the lists are very different syntactically but that shouldn't be the issue. In the C# code a List<Tuple<..>> is accepted by the compiler but in the managed C++ code it is not. Am I doing this wrong/am I using the wrong data types? Any help would be awesome!

Était-ce utile?

La solution

Try something like this:

using namespace System::Collections::Generic;

// A List<Tuple<int, float>> in managed C++
public ref class TupleTest
{
public:
    static List<Tuple<int, float>^>^ GetTuple() {
        List<Tuple<int, float>^>^ ret = gcnew List<Tuple<int,float>^>();
        Tuple<int,float>^ t = gcnew Tuple<int,float>(5, 2.6);
        ret->Add(t);
        return ret;
    }
};

Note: you could use Int32 and Single (or Double) instead of int/float if you like.

EDIT: Notice the ^ operators. Those denote C++/CLI reference types. You'll be using them a lot! (It's kinda like * for pointers in regular C++, but means GC'ed reference type)

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top