Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 line except "ref". What does that mean?

like image 221
Timothy Baldridge Avatar asked Sep 23 '11 14:09

Timothy Baldridge


3 Answers

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^.

like image 111
Ben Voigt Avatar answered Sep 21 '22 02:09

Ben Voigt


"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.

like image 35
ReinstateMonica Larry Osterman Avatar answered Sep 20 '22 02:09

ReinstateMonica Larry Osterman


ref in this case stands for reference counting. Classes using ref are WinRT component which have reference count machanisms out of the box.

like image 38
Claudio Junior Avatar answered Sep 20 '22 02:09

Claudio Junior