Requirements for STL/CLR Container Elements
Say I've got a class like the following:
public ref class MyClass {};
I would like to have a vector of objects of such a class:
cliext::vector<MyClass ^> ^myVector;
开发者_开发百科
I understand that this is a vector of handles to reference types, isn't it? I mean, this is not a vector of reference types, so I don't need to define any of these requirements:
Requirements for STL/CLR Container Elements
These requirements would be necessary in case I defined cliext::vector<MyClass> ^myVector
, because then a copy of each element would be stored in the container and blah blah.
Is all this right? Because I'm getting a NullReferenceException
when I retrieve an element from the vector. I can see the object is properly constructed, but when I get it back from the vector all its members are undefined.
The STL/CLR library was delivered very late, three years after the original promised shipping date. It was quickly obvious to everybody why, it combined all the disadvantages of the native C++ container classes with the disadvantages of managed memory management. There is no upside, the library doesn't have a single redeeming value. Its compatibility with the C++ classes is only a liability, preventing you from doing the right thing. Review this page for the horror story.
Do not invest any of your time in making this work, it is utterly wasted effort. Use the classes in the System::Collections::Generic namespace.
精彩评论