c++ cli - Where is this managed object stored? -
c++ cli - Where is this managed object stored? -
value class valbase { public: int a; }; ref class refbase { public: int a; }; int main(array<system::string ^> ^args) { refbase^ refbase1 = gcnew refbase; //legal. ref type managed obj created on clr heap. valbase^ valbase1 = gcnew valbase; //legal. value type managed obj created on clr heap. refbase* refbase2 = new refbase; //illegal: new cannot used on managed ref class valbase* valbase2 = new valbase; //this compiles okay "managed object" stored ? clr heap or native heap ? }
in lastly assignment managed object stored ? totally new c++ cli. also, true value types should utilize stack semantics create code efficient ? i.e instead of valbase^ valbase1 = gcnew valbase, should utilize valbase valbase1;
just add together fellow member of reference type value type:
value class valbase { public: string^ s; int a; }; ... valbase* = new valbase;
and compiler tells stored:
error c3255: 'valbase' : cannot dynamically allocate value type object on native heap
the semantics simple enough, after can store value type on stack. if can go on stack can go on native heap well. long doesn't contain objects need found garbage collector anyway. why c3255 there. value types exist in .net framework reason, storing stuff on stack inexpensive , makes code efficient.
but because possible store on native heap doesn't create useful so. same true valbase^ valbase1 = gcnew valbase;
, stores boxed value on managed heap. re-create of value embedded in system::object. boxing useful because allows pretending value types inherit object. isn't cheap, never you'd want no reason.
c++-cli
Comments
Post a Comment