C ++ / CX ( Component Extensions ) - C ++ language extensions implemented in Microsoft compilers. Allows you to write C ++ programs for the new Windows Runtime ( WinRT ) platform. They bring in syntactic and library abstractions that work with the WinRT COM programming model in a natural way for C ++ programmers.
The syntax is similar to that used in C ++ / CLI , but C ++ / CX programs run on Windows Runtime and are compiled into machine code rather than the Common Language Runtime and the managed code bytecode.
Content
Extension Syntax
C ++ / CX introduces syntax extensions for programming for Windows Runtime. Generic platform-independent syntax compatible with the C ++ 11 standard.
Objects
WinRT objects are created (activated) using ref new and assigned to variables declared with the ^ notation inherited from C ++ / CLI.
Foo ^ foo = ref new Foo ();
The WinRT variable is a pair of pointers: a pointer to a table of virtual methods and a pointer to the internal data of the object.
Link Counting
WinRT objects use reference counting and behave similarly to regular C ++ objects used with smart pointers (std :: unique_ptr). An object is subject to deletion when no links will lead to it.
Garbage collection is not possible, but the gcnew keyword has been reserved for potential future use.
Classes
Runtime Classes
A special type of runtime classes that can contain constructions of extending components. They are sometimes called ref classes , because they are declared using the ref class construct:
public ref class MyClass
{
};
Partial classes
C ++ / CX introduces the concept of partial classes. With their use, it is possible to define one class in parts in several source files. For example, they can be used to implement code generators for the XAML GUI. At compile time, parts of the class are combined into a single class.
.NET languages such as C # have supported this for many years. However, they were not included in the 2011 C ++ standard and cannot be used in pure C ++ 11.
The file generated and updated by the GUI designer must not be manually changed by the programmer. Note the use of the partial keyword.
// foo.private.h
#pragma once
partial ref class foo
{
private :
int id_ ;
Platform :: String ^ name_ ;
};
A file in which the programmer describes the user interface logic. The header file generated earlier is connected via include. The partial keyword is not needed in this file.
// foo.public.h
#pragma once
#include "foo.private.h"
ref class foo
{
public :
int GetId ();
Platform :: String ^ GetName ();
};
This file shows the implementation of the members of a partial class.
// foo.cpp
#include "pch.h"
#include "foo.public.h"
int foo :: GetId () { return id_ ;}
Platform :: String ^ foo :: GetName { return name_ ;}
Generics
Windows Runtime and therefore C ++ / CX support runtime generics . Information about generic types is stored in metadata and instantiated at runtime (unlike classic C ++ templates that are processed at compile time).
generic < typename T >
public ref class bag
{
property T Item ;
};
Metadata
All WinRT programs describe the classes declared in them and their members through metadata. Their format is the same as the Common Language Infrastructure (CLI) metadata format that was created for the .NET Framework . Thanks to this, it is possible to combine codes written in C ++ / CX, CLI and Javascript .
Runtime Library
C ++ / CX has a set of libraries designed to work with Windows Runtime. They implement the integration of C ++ STL and WinRT functionality.
See also
- Windows runtime