I know in C++/CLI one cannot use unmanaged types when defining a managed class:
public struct Unmanaged
{
int x;
int y;
};
public ref class Managed
{
int one;
Unmanaged two; //error C4368
};
I do not understand why though. Unmanaged
is simply a collection of native types - its size is known, surely it (and by it I mean the block of memory that defines it) would be moved around with the 'block of memory' that is Managed
inside the 'managed heap', and whatever offset is stored in the metadata will remain valid, no? Just as if an integer or a float were declared?
Why can we not mix types?
Mixed type actually refers to the mixed memory models. Unmanaged types go on the heap, managed types go in the garbage collected heap, so when you embed an unmanaged type in a managed, it would require memory on both heaps, which is why you do this sort of thing with a pointer. The pointer is managed, the value it points to isn't.
I was curious myself, so I gathered up my google and found this.
http://blogs.msdn.com/b/branbray/archive/2005/07/20/441099.aspx
Guy seems to know what he's talking about.
Good question though...