天天看點

ref class VS value class

什麼是C++ /CX? 首先要明白它跟C++ 0x/11以及C++ /CLR是完全不同的東西。C++ 0x/11是目前最新的C++标準庫,而C++ /CX其實是微軟在Win8開發平台下,對C++語言的一種擴充。C++ /CLR是微軟為了C++能在.Net下運作,針對CLR,雖然也是對C++的擴充,但它編譯後是托管于CLR的,屬于Managed C++。而C++ /CX則屬于Native C++,它不使用CLR也沒有垃圾回收機制。雖然C++ /CX有些新文法特性是直接從/CLR借鑒過來的,但是從底層實作上來看,它們是完全不同的兩種擴充。

ref class or struct will create reference types. They are created on the managed heap and only references (like pointers when you think of it natively) to those objects are stored and passed.

value class or struct are value types. When you pass them around as parameters or as members the whole memory block will be transferred. So you sould use value types only for small data types.

ref class是引用類型,存放在堆記憶體,引用計數開銷大

value class是值類型 存放在棧記憶體,但是作為參數傳遞時如果資料太大效率低,是以适用于small資料類型