Lably
Skip to content

C++/CX

  • 4 min read
  • 4 chapters

Related on Lably

Through Compiler, Class (programming) and Graphical user interface

The language extensions borrow syntax from C++/CLI but target the Windows Runtime Universal Windows Platform native code instead of the Common Language Runtime and managed code. It brings a set of syntax and library abstractions that project COM's WRL subset-based WinRT programming model in a way that is intuitive to C++/CLI managed extensions' coders.

It is possible to call the Windows Runtime from native ISO C++ via the lower level Windows Runtime C++ Template Library (WRL). However, WRL is also superseded by C++/WinRT.

Extension syntax

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.

<span id="mwKg"></span><span class="n" id="mwKw">Foo</span><span class="o" id="mwLA">^</span><span class="w" id="mwLQ"> </span><span class="n" id="mwLg">foo</span><span class="w" id="mwLw"> </span><span class="o" id="mwMA">=</span><span class="w" id="mwMQ"> </span><span class="n" id="mwMg">ref</span><span class="w" id="mwMw"> </span><span class="k" id="mwNA">new</span><span class="w" id="mwNQ"> </span><span class="n" id="mwNg">Foo</span><span class="p" id="mwNw">();</span>

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.

<span id="mwSg"></span><span class="k" id="mwSw">public</span><span class="w" id="mwTA"> </span><span class="n" id="mwTQ">ref</span><span class="w" id="mwTg"> </span><span class="k" id="mwTw">class</span><span class="w" id="mwUA"> </span><span class="nc" id="mwUQ">MyClass</span> <span class="p" id="mwUg">{</span> <span class="p" id="mwUw">};</span>

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 have not yet made it into the C++ standard and cannot therefore be used, even in C++20.

A file that is generated and updated by the GUI-designer, and thus should not be modified by the programmer. Note the keyword partial.

<span id="mwYg"></span><span class="c1" id="mwYw">// foo.private.h</span> <span class="cp" id="mwZA">#pragma once</span> <span class="n" id="mwZQ">partial</span><span class="w" id="mwZg"> </span><span class="n" id="mwZw">ref</span><span class="w" id="mwaA"> </span><span class="k" id="mwaQ">class</span><span class="w" id="mwag"> </span><span class="nc" id="mwaw">foo</span> <span class="p" id="mwbA">{</span> <span class="k" id="mwbQ">private</span><span class="o" id="mwbg">:</span> <span class="w" id="mwbw"> </span><span class="kt" id="mwcA">int</span><span class="w" id="mwcQ"> </span><span class="n" id="mwcg">id_</span><span class="p" id="mwcw">;</span> <span class="w" id="mwdA"> </span><span class="n" id="mwdQ">Platform</span><span class="o" id="mwdg">::</span><span class="n" id="mwdw">String</span><span class="o" id="mweA">^</span><span class="w" id="mweQ"> </span><span class="n" id="mweg">name_</span><span class="p" id="mwew">;</span> <span class="p" id="mwfA">};</span>

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.

<span id="mwgQ"></span><span class="c1" id="mwgg">// foo.public.h</span> <span class="cp" id="mwgw">#pragma once</span> <span class="cp" id="mwhA">#include</span><span class="w" id="mwhQ"> </span><span class="cpf" id="mwhg">"foo.private.h"</span> <span class="n" id="mwhw">ref</span><span class="w" id="mwiA"> </span><span class="k" id="mwiQ">class</span><span class="w" id="mwig"> </span><span class="nc" id="mwiw">foo</span> <span class="p" id="mwjA">{</span> <span class="k" id="mwjQ">public</span><span class="o" id="mwjg">:</span> <span class="w" id="mwjw"> </span><span class="kt" id="mwkA">int</span><span class="w" id="mwkQ"> </span><span class="n" id="mwkg">GetId</span><span class="p" id="mwkw">();</span> <span class="w" id="mwlA"> </span><span class="n" id="mwlQ">Platform</span><span class="o" id="mwlg">::</span><span class="n" id="mwlw">String</span><span class="o" id="mwmA">^</span><span class="w" id="mwmQ"> </span><span class="n" id="mwmg">GetName</span><span class="p" id="mwmw">();</span> <span class="p" id="mwnA">};</span>

This is the file in which the members of the partial class are implemented.

<span id="mwoA"></span><span class="c1" id="mwoQ">// foo.cpp</span> <span class="cp" id="mwog">#include</span><span class="w" id="mwow"> </span><span class="cpf" id="mwpA">"pch.h"</span> <span class="cp" id="mwpQ">#include</span><span class="w" id="mwpg"> </span><span class="cpf" id="mwpw">"foo.public.h"</span> <span class="kt" id="mwqA">int</span><span class="w" id="mwqQ"> </span><span class="nf" id="mwqg">foo::GetId</span><span class="p" id="mwqw">()</span><span class="w" id="mwrA"> </span><span class="p" id="mwrQ">{</span><span class="k" id="mwrg">return</span><span class="w" id="mwrw"> </span><span class="n" id="mwsA">id_</span><span class="p" id="mwsQ">;}</span> <span class="n" id="mwsg">Platform</span><span class="o" id="mwsw">::</span><span class="n" id="mwtA">String</span><span class="o" id="mwtQ">^</span><span class="w" id="mwtg"> </span><span class="n" id="mwtw">foo</span><span class="o" id="mwuA">::</span><span class="n" id="mwuQ">GetName</span><span class="w" id="mwug"> </span><span class="p" id="mwuw">{</span><span class="k" id="mwvA">return</span><span class="w" id="mwvQ"> </span><span class="n" id="mwvg">name_</span><span class="p" id="mwvw">;}</span>

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.

<span id="mwxg"></span><span class="n" id="mwxw">generic</span><span class="o" id="mwyA"><</span><span class="k" id="mwyQ">typename</span><span class="w" id="mwyg"> </span><span class="nc" id="mwyw">T</span><span class="o" id="mwzA">></span><span class="w" id="mwzQ"> </span> <span class="k" id="mwzg">public</span><span class="w" id="mwzw"> </span><span class="n" id="mw0A">ref</span><span class="w" id="mw0Q"> </span><span class="k" id="mw0g">class</span><span class="w" id="mw0w"> </span><span class="nc" id="mw1A">bag</span><span class="w" id="mw1Q"> </span> <span class="p" id="mw1g">{</span> <span class="w" id="mw1w"> </span><span class="n" id="mw2A">property</span><span class="w" id="mw2Q"> </span><span class="n" id="mw2g">T</span><span class="w" id="mw2w"> </span><span class="n" id="mw3A">Item</span><span class="p" id="mw3Q">;</span> <span class="p" id="mw3g">};</span>

Sign in to keep reading

3 more chapters are free with a Lably account.

More in Computing and communication