auto foo = ref new Foo(); What is "ref"?
I was watching a video from, //build/ and several of the MS developers were using a syntax like this in their C++11 programs:
auto foo = ref new Foo();
I understand what everything does in this lin开发者_如何学编程e except "ref". What does that mean?
The forthcoming Visual C++ compiler adds this syntax for dealing with WinRT objects (which are in turn the next generation of COM, what have we gone through now? COM, DCOM, COM+, ActiveX, ...)
That line is nearly equivalent to:
com_ptr_t<Foo> foo = CreateInstance<Foo>();
But there's a new version of com_ptr_t
as well, using the syntax Foo^
.
"ref new" is a 2 token keyword. It instructs the compiler to instantiate a windows runtime object and automatically manage the lifetime of the object (via the "^" operator).
Instantiating a windows runtime object causes an allocation, but it does not have to be on the heap.
ref in this case stands for reference counting. Classes using ref are WinRT component which have reference count machanisms out of the box.
精彩评论