Muokkaa

Jaa


Quick Reference (C++/CX)

The Windows Runtime supports Universal Windows Platform (UWP) apps. These apps execute only in a trustworthy operating system environment, use authorized functions, data types, and devices, and are distributed through the Microsoft Store. The C++/CX simplify the writing of apps for the Windows Runtime. This article is a quick reference; for more complete documentation, see Type system.

When you build on the command line, use the /ZW compiler option to build a UWP app or Windows Runtime component. To access Windows Runtime declarations, which are defined in the Windows Runtime metadata (.winmd) files, specify the #using directive or the /FU compiler option. When you create a project for a UWP app, Visual Studio by default sets these options and adds references to all Windows Runtime libraries.

Quick reference

Concept Standard C++ C++/CX Remarks
Fundamental types C++ fundamental types. C++/CX fundamental types that implement fundamental types that are defined in the Windows Runtime. The default namespace contains C++/CX built-in, fundamental types. The compiler implicitly maps C++/CX fundamental types to standard C++ types.

The Platform family of namespaces contains types that implement fundamental Windows Runtime types.
bool bool An 8-bit Boolean value.
wchar_t, char16_t char16 A 16-bit nonnumeric value that represents a Unicode (UTF-16) code point.
short

unsigned short
int16

uint16
A 16-bit signed integer.

A 16-bit unsigned integer.
int

unsigned int
int

uint32
A 32-bit signed integer.

A 32-bit unsigned integer.
long long -or- __int64

unsigned long long
int64

uint64
A 64-bit signed integer.

A 64-bit unsigned integer.
float, double float32, float64 A 32-bit or 64-bit IEEE 754 floating-point number.
enum enum class

-or-

enum struct
A 32-bit enumeration.
(Doesn't apply) Platform::Guid A 128-bit nonnumeric value (a GUID) in the Platform namespace.
std::time_get Windows::Foundation::DateTime A date-time structure.
(Doesn't apply) Windows::Foundation::TimeSpan A timespan structure.
(Doesn't apply) Platform::Object^ The reference-counted base object in the C++ view of the Windows Runtime type system.
std::wstring

L"..."
Platform::String^ Platform::String^ is a reference-counted, immutable, sequence of Unicode characters that represent text.
Pointer Pointer to object (*):

std::shared_ptr
Handle-to-object (^, pronounced "hat"):

T^ identifier
All Windows Runtime classes are declared by using the handle-to-object modifier. Members of the object are accessed by using the arrow (->) class-member-access operator.

The hat modifier means "pointer to a Windows Runtime object that is automatically reference counted." More precisely, handle-to-object declares that the compiler should insert code to manage the object's reference count automatically, and delete the object if the reference count goes to zero
Reference Reference to an object (&):

T& identifier
Tracking reference (%):

T% identifier
Only Windows Runtime types can be declared by using the tracking reference modifier. Members of the object are accessed by using the dot (.) class-member-access operator.

The tracking reference means "a reference to a Windows Runtime object that is automatically reference counted." More precisely, a tracking reference declares that the compiler should insert code to manage the object's reference count automatically. The code deletes the object if the reference count goes to zero.
Dynamic type declaration new ref new Allocates a Windows Runtime object and then returns a handle to that object.
Object lifetime management delete identifier

delete[] identifier
(Invokes the destructor.) Lifetime is determined by reference counting. A call to delete invokes the destructor but itself doesn't free memory.
Array declaration T identifier[]

std::array identifier
Array<T^>^ identifier( size )

-or-

WriteOnlyArray<T^> identifier( size )
Declares a one-dimensional modifiable or write-only array of type T^. The array itself is also a reference-counted object that must be declared by using the handle-to-object modifier.

(Array declarations use a template header class that is in the Platform namespace.)
Class declaration class identifier {}

struct identifier {}
ref class identifier {}

ref struct identifier {}
Declares a runtime class that has default private accessibility.

Declares a runtime class that has default public accessibility.
Structure declaration struct identifier {}

(that is, a Plain Old Data structure (POD))
value class identifier {}

value struct identifier {}
Declares a POD struct that has default private accessibility.

A value class can be represented in Windows metadata, but a standard C++ class can't be.

Declares a POD struct that has default public accessibility.

A value struct can be represented in Windows metadata, but a standard C++ struct can't be.
Interface declaration abstract class that contains only pure virtual functions. interface class identifier {}

interface struct identifier {}
Declares an interface that has default private accessibility.

Declares an interface that has default public accessibility.
Delegate std::function public delegate return-type delegate-type-identifier ( [ parameters ] ); Declares an object that can be invoked like a function call.
Event (Doesn't apply) event delegate-type-identifier event-identifier;

delegate-type-identifier delegate-identifier = ref new delegate-type-identifier( this [, parameters] );

event-identifier += *delegate-identifier;

-or-

EventRegistrationToken token-identifier = object.event-identifier += delegate-identifier;

-or-

auto token-identifier = object.event-identifier::add( delegate-identifier );

object.event-identifier -= token-identifier;

-or-

object.event-identifier::remove( token-identifier );
Declares an event object, which stores a collection of event handlers (delegates) that are called when an event occurs.

Creates an event handler.

Adds an event handler.

Adding an event handler returns an event token (token-identifier). If you intend to explicitly remove the event handler, you must save the event token for later use.

Removes an event handler.

To remove an event handler, you must specify the event token that you saved when the event handler was added.
Property (Doesn't apply) property T identifier;

property T identifier[ index ];

property T default[ index ];
Declares that a class or object member function is accessed by using the same syntax that's used to access a data member or indexed array element.

Declares a property on a class or object member function.

Declares an indexed property on an object member function.

Declares an indexed property on a class member function.
Parameterized types templates generic <typename T> interface class identifier {}

generic <typename T > delegate [return-type] delegate-identifier() {}
Declares a parameterized interface class.

Declares a parameterized delegate.
Nullable value types std::optional<T> Platform::IBox <T> Enables variables of scalar types and value structs to have a value of nullptr.

See also

C++/CX language reference