C++/CX introduces syntax extensions for programming for the Windows Runtime. The overall non platform-specific syntax is compatible with the
C++11 standard.
Objects WinRT objects are created, or
activated, using ref new and assigned to variables declared with the ^ (hat) notation inherited from C++/CLI. Foo^ foo = ref new Foo(); A WinRT variable is simply a pair of a pointer to
virtual method table and pointer to the object's
internal data.
Reference counting A WinRT object is
reference counted and thus handles similarly to ordinary C++ objects enclosed in
shared_ptrs. An object will be deleted when there are no remaining references that lead to it. There is no
garbage collection involved. Nevertheless, the keyword gcnew has been reserved for possible future use.
Classes Runtime classes There are special kinds of
runtime classes that may contain component extension constructs. These are simply referred to as
ref classes because they are declared using ref class. public ref class MyClass { };
Partial classes C++/CX introduces the concept of
partial classes. The feature allows a single class definition to be split across multiple files, mainly to enable the
XAML graphical user interface design tools to auto-generate code in a separate file in order not to break the logic written by the developer. The parts are later merged at compilation.
.NET languages like
C# have had this feature for many years. Partial classes do not exist in the
C++ standard and cannot therefore be used. A file that is generated and updated by the GUI-designer, and thus should not be modified by the programmer. Note the keyword partial. // Foo.private.h • pragma once using Platform::String; partial ref class Foo { private: int id; String^ name; }; The file where the programmer writes user-interface logic. The header in which the compiler-generated part of the class is defined is imported. Note that the keyword partial is not necessary. // Foo.public.h • pragma once • include "Foo.private.h" using Platform::String; ref class Foo { public: int getId(); String^ getName(); }; This is the file in which the members of the partial class are implemented. // Foo.cpp • include "pch.h" • include "Foo.public.h" using Platform::String; int Foo::getId() { return id; } String^ Foo::getName() { return name; }
Generics Windows Runtime and thus C++/CX supports runtime-based
generics. Generic type information is contained in the metadata and instantiated at runtime, unlike
C++ templates which are compile-time constructs. Both are supported by the compiler and can be combined. generic public ref class Bag { property T Item; }; == Metadata ==