开发者

CLI-c++ references and handles

开发者 https://www.devze.com 2023-04-04 01:40 出处:网络
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,

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号