CLI-c++ references and handles
im really sorry for this dumb question, is there actual references in .net version of c++? in native c++ there are pointers (*
, **
) and refe开发者_运维问答rences (&
, &&
). as far as i know, in cli/c++ there are: interior pointers (interior_ptr<>
), pinning pointers (pin_ptr<>
), handles (^
), tracking references (^%
).
but is there an equivalent for actual native c++ reference?
C++/CLI supports regular pointers and references just like C++ does. But not to objects of a managed class. That cannot work by design, managed objects get moved in memory when the garbage collector compacts the heap. This is checked well at compile time, the compiler will tell you when you get it wrong. Managed pointer and reference syntax is ^ and %. You can convert a managed pointer to an unmanaged one with the pin_ptr<> class. Whose side-effect is that the object gets pinned so it cannot be moved and invalidate the pointer.
Terms like 'tracking reference' and 'reference to a handle' are a useful semantic description of a managed pointer. It doesn't otherwise have anything to do with what happens at runtime. They are regular pointers just like C++ pointers and have the same perf. There is no intermediary 'handle'. The only difference is that the garbage collector can find them back and update their value.
精彩评论