1

I'm coming from C# background, learning C++, specifically on the Windows Phone 8 platform.

Many code samples (installed with the SDK) show usage of the Hat operator ^ (Reference here: Types that wear hats).

For example:

void PhoneDX::Initialize(CoreApplicationView^ applicationView)
{
    // ... function body
}

I am wondering:

  • Why are most of the pointers being defined in that manner, specifically on Windows Phone 8 ?

  • Is that syntax mandatory? Suppose i am using a C++ native library from another platform (that doesn't use this syntax). Should it work with no issues?

lysergic-acid
  • 19,570
  • 21
  • 109
  • 218
  • 1
    You may wanna point out that the hat operator looks like this: `^`. Also, that is not valid _C++_ but _C++/CX_ – K-ballo Jan 10 '13 at 23:55
  • Thanks, I have added that + small code illustrating my question. – lysergic-acid Jan 10 '13 at 23:58
  • 1
    Have you read the series [from the start](http://blogs.msdn.com/b/vcblog/archive/2012/08/29/cxxcxpart00anintroduction.aspx)? I tihnk both of your questions answer themselves once you understand why they exist in the first place. – GManNickG Jan 11 '13 at 00:10
  • 2
    While I don't want to dissuade you from learning to develop for our platforms, I strongly recommend learning C++ first by getting a good book like [_C++ Primer 5ed_](http://www.amazon.com/Primer-5th-Stanley-B-Lippman/dp/0321714113) or one of the other introductory books recommended in [The C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), _then_, once you have a firm grasp of the language, learn how to apply that to the specific platform. – James McNellis Jan 11 '13 at 01:35
  • 1
    (If you have experience in C#, learning C++ will likely be fairly easy, but it's very, very important to understand the essentials, like how the C++ object model works, before writing even moderately complex code. C++ code looks syntactically similar to C# in many ways, but under the hood things work very differently, and idiomatic C++ is quite different from idiomatic C#.) – James McNellis Jan 11 '13 at 01:36

2 Answers2

3

A hat is a compiler-supported smart pointer type that is designed to make Windows Runtime types easier to work with from C++ code. As discussed in "Types That Wear Hats" and the other articles in that series, the C++/CX language extensions are optional: any code that can be written using C++/CX can be written in C++ without using the language extensions, albeit at greater code complexity and verbosity.

The key here is that hats are designed to facilitate code that makes use of Windows Runtime types. In general, you should confine your use of C++/CX and Windows Runtime types to the boundary of your components: most of your code should be standard, portable, normal C++ code. C++/CX should be used (1) to wrap C++ code to make it consumable through the Windows Runtime and (2) to use other Windows Runtime components from your component.

So, yes, the syntax is optional, but you should strongly consider using it when writing code that must work with Windows Runtime types. You should be able to use any ordinary C++ code, without modification, with the caveat that Windows Store apps and Windows Phone apps run with low privileges and some facilities are not available (e.g., there is no console, so console I/O doesn't work, and the runtime provides specialized process lifetime management facilities, so calling exit is a bad idea).

James McNellis
  • 348,265
  • 75
  • 913
  • 977
1

You might have a harder time grasping it since it's somewhat of a compound leap from (1) C# references to (4) C++/CX hats, with a stop in the middle for (2) C++ pointers then (3) reference counted objects.

The ^ smart pointers are a language extension, not part of standard C++, for handling the Windows Runtime types (which are reference counted)

so answering your points:

  • If you're seeing them a lot with Windows Phone 8, it's because ^ is used for Windows Runtime types, which would be used a lot in that platform samples (since the samples are trying to demonstrate that platform's api and features).
  • You would need to use conventions for that library, which would probably require that you use the types it defines (if it has its own smart pointers) or standard/regular pointers (i.e. *) or Standard Library smart pointers (i.e. shared_ptr).

Some concepts that should help you understanding this would be the lifetime of C++ objects, deterministic destruction (vs. waiting for a garbage collector to kick in), reference counting, stack/static vs. heap/dynamic allocation of objects.

Community
  • 1
  • 1
frozenkoi
  • 3,228
  • 22
  • 33