Convert std :: vector <> :: iterator to .NET interface in C ++ / CLI

I am wrapping my own C ++ class, which has the following methods:

class Native
{
    public:
    class Local
    {
        std::string m_Str;
        int m_Int;
    };

    typedef std::vector<Local> LocalVec;
    typedef LocalVec::iterator LocalIter;

    LocalIter BeginLocals();
    LocalIter EndLocals();

    private:
        LocalVec m_Locals;
};

1) What is the ".NET method" to represent the same interface? The only method returning an array <>? Does the array <> have generic iterators so that I can implement BeginLocals () and EndLocals ()?

2) Should Local be declared as a struct value in the .NET shell?

I would really like to introduce a class with a .NET class, but I'm very new to the managed world - and this type of information is frustrating for Google for ...

+5
source share
2

" .net", IEnumerable <T> IEnumerator <T> .

  vector<int> a_vector;
  vector<int>::iterator a_iterator;
  for(int i= 0; i < 100; i++)
  {
    a_vector.push_back(i);
  }

  int total = 0;
  a_iterator = a_vector.begin();
  while( a_iterator != a_vector.end() ) {
    total += *a_iterator;
    a_iterator++;
  }

( #)

List<int> a_list = new List<int>();
for(int i=0; i < 100; i++)
{
  a_list.Add(i);
}
int total = 0;
foreach( int item in a_list)
{
  total += item;
}

( IEnumerator foreach):

List<int> a_list = new List<int>();
for (int i = 0; i < 100; i++)
{
    a_list.Add(i);
}
int total = 0;
IEnumerator<int> a_enumerator = a_list.GetEnumerator();
while (a_enumerator.MoveNext())
{
    total += a_enumerator.Current;
}

, foreach .net.

, , ".net" List < > . , IEnumerable <T> / ICollection <T> .

# , :

public class Native
{
  public class Local
  { 
     public string m_str;
     public int m_int;
  }

  private List<Local> m_Locals = new List<Local>();

  public List<Local> Locals
  {
    get{ return m_Locals;}
  }
}

foreach( Local item in someNative.Locals)  
{
 ... 
}
+5

@Phillip - , .

Nish ++/CLI , , indexed, const , . - :

public ref class Managed
{
    public:
    ref class Local
    {
        String^ m_Str;
        int m_Int;
    };

    property const Local^ Locals[int]
    {
        const Local^ get(int Index)
        {
            // error checking here...
            return m_Locals[Index];
        }
    };

    private:
        List<Local^> m_Locals;
};
0

All Articles