Export (0) Print
Expand All

Quick Reference (C++/CX)

The Windows Runtime supports Windows Store apps that execute only in a trustworthy operating system environment, use authorized functions, data types, and devices, and are distributed through the Windows Store. The Visual C++ component extensions (C++/CX) simplify the writing of apps for the Windows Runtime. This article is a quick reference; for more complete documentation, see Type System (C++/CX) and Component Extensions for Runtime Platforms.

When you build on the command line, use the /ZW compiler option to build a Windows Store 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 Windows Store app, Visual Studio by default sets these options and adds references to all Windows Runtime libraries.

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

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.

(Does not apply)

Platform::Guid

A 128-bit nonnumeric value (a GUID) in the Platform namespace.

std::time_get

Windows::Foundation::DateTime

A date-time structure.

(Does not apply)

Windows::Foundation::TimeSpan

A timespan structure.

(Does not 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 to…

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 automatically manage the object's reference count, and delete the object if the reference count goes to zero.

Reference to…

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 automatically manage the object's reference count, and delete 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 does not 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 cannot be.

Declares a POD struct that has default public accessibility.

A value struct can be represented in Windows metadata, but a standard C++ struct cannot 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

(Does not apply)

event delegate-type-identifier event-identifier ;

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

event-identifier += delegate-identifier ;

-or-

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

-or-

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

obj . event-identifier -= token-identifier ;

-or-

obj . 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

(Does not 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

boost::optional<T>

Platform::IBox <T>

Enables variables of scalar types and value structs to have a value of nullptr.

Show:
© 2014 Microsoft