Jan 132013
 
 January 13, 2013  Posted by at 11:20 pm Not So Stupid Questions, Uncategorized  Add comments

[To celebrate my first year of programming I will ask a ‘stupid’ questions daily on my blog for a year, to make sure I learn at least 365 new things during my second year as a developer]

What is a hat type?

What is a hat type?

I’m getting more and more comfy with my latest crush WinRT, and as I do I’ve decided to learn more about the language projections even though  I wont necessarily learn to be fluent in all of them (I am of course talking about C++ here). The step by step guide I did in C# and XAML for Windows Store Apps will be redone in JavaScript and Html, and I’ve been having  a big of a read in RE to C++ and Windows Store Apps so I’ll be able to answer or know where to look for answers, if I get any C++ and WinRT questions during the sessions I’ll be giving the next few months on Windows Store Apps.

So a first question, which fits very nicely with the previous (but still ongoing, pointer series some questions: What is a pointer?) is what is a hat type in C++/CX?

Example:  ArrayList ^ arr = gcnew ArrayList();

The ^ actually indicates a managed pointer. What it does is that it declares a handle to an object on the managed heap, it used to be _gc but the hat ^ (the handle declarator) has replaced it. It points to the whole object , and you cannot do pointer arithmetic, but on the other hand it gives you type conversion capabilities. You should use the gcnew keyword which creates a new instance of a managed type so it will automatically be garbage collected.

  3 Responses to “Stupid Question 126: What is a hat type or handle declarator in C++/CX?”

  1. One correction here as I think when you talk about C++/Cx and Windows Store apps it needs to be said that there is no managed code/CLR involved here. The ‘hat’ in a WinRT app is a smart pointer effectively, but there is no CLR GC involved.

    So:

    Button^ m_Button = new Button();

    No managed code involved.

  2. That’s a good description of C++/CLI, the generation of managed C++ introduced with Visual Studio 2005. However in C++/CX the syntax was reused with a subtly different meaning. A C++/CX hat variable is a reference counted pointer natively understood by the compiler, but otherwise similar to ATL’s CComPtr.

    (I first learned this in one of Herb Sutter’s Build 2011 talks (see slide 12 in particular), where I was convinced he was confused until I learned what C++/CX really was.)

  3. You are all right, yet wrong also: The hat, or handle declarator, has a very different underlying implementation depending on whether your code is native (compiled with /ZW) or managed (compiled with /CLI). The native version is basically a COM smart pointer, and the CLI version is a handle to a garbage collected object. Also, one should use “ref new” instead of gcnew, which is what ref new does under CLI anyway.

    To confuse matters more, some compiler errors say “/CLI is required” when /ZW will work also.

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)

What is 15 + 9 ?
Please leave these two fields as-is:
IMPORTANT! To be able to proceed, you need to solve the following simple math (so we know that you are a human) :-)