For faster navigation, this Iframe is preloading the Wikiwand page for C++/CX.

C++/CX

此條目没有列出任何参考或来源。 (2014年9月23日)維基百科所有的內容都應該可供查證。请协助補充可靠来源改善这篇条目。无法查证的內容可能會因為異議提出而被移除。

C++/CX (Visual C++ Component Extensions,Visual C++ 组件扩展) 是微软的C++编译器对C++的扩展,使得程序员可以比较方便地编写面向Windows Runtime(WinRT)的程序。这个语言规范引入了一系列语法和类库抽象,以对C++程序员来说比较自然的方式暴露了基于组件对象模型(COM)的WinRT编程范型的接口。

这个语言扩展从C++/CLI借用了语法,但是是面向WinRT和原生码而不是通用语言运行库托管代码

扩展语法

C++/CX为WinRT编程引入了新的语法。和平台独立的语法和C++11标准兼容。

物件 (Objects)

WinRT物件的创建方式是通过ref new关键字,并赋值给具有^(帽子)注记的类型的变量,这个帽子标记是继承自C++/CLI。

Foo^ foo = ref new Foo();

一个WinRT变量仅仅是一对指针,一个指向虚函数表(Virtual method table页面存档备份,存于互联网档案馆)),另一个指向物件的内部数据(Opaque pointer页面存档备份,存于互联网档案馆))。

引用计数 (Reference counting)

一个WinRT物件是使用引用计数模式来维护其生命期的,处理方式类似于被智能指针封装的常规C++物件。物件在没有引用时销毁。尽管没有垃圾回收机制,但是语言规范也保留了gcnew关键字以备以后使用。

類型 (Classes)

運行類型 (Runtime classes)

運行類型包含了元件擴展的建構函式。這類型也被簡單地稱為 ref classes,因為它們是用 ref class 創建的。

public ref class MyClass
{

};
局部类型 (Partial classes)

C++/CX引入了局部类型的概念。这个特性使得一个类的定义可以被分离到多个文件,使得XAML图形用户界面设计工具能够自动生成部分代码到一个单独的文件,以避免干扰程序员写的代码。这些局部类型在编译时合并。这个特性在类似C#之类的CLI语言中已经存在很多年。局部类型并未成为C++标准,所以并不能在纯C++11代码中使用。

这里是一个设计工具生成的局部类型示例:

// foo.private.h
#pragma once

partial ref class foo
{
private:
   int id_;
   Platform::String^ name_;
};

这里是一个程序员编写的类示例。生成的局部类型在开头被导入,注意这里partial关键字不是必要的:

// foo.public.h
#pragma once
#include "foo.private.h"

ref class foo
{
public:
   int GetId();
   Platform::String^ GetName();
};

这是局部类型的实现部分代码的示例:

// foo.cpp
#include "pch.h"
#include "foo.public.h"

int foo::GetId() {return id_;}
Platform::String^ foo::GetName {return name_;}

泛型 (Generics)

WinRT支持泛型,所以C++/CX也支持。泛型类的信息被保留在元数据内,在运行时才实例化,而不是像C++模板那样在编译时示例化。编译器支持在一个类型中同时使用泛型和模板。

泛型类示例:

generic<typename T> 

public ref class bag 
{
     property T Item;
};

元数据 (Metadata)


所有WinRT程序都通过元数据来描述其中的类型。元数据的格式和通用语言架构(CLI)为.NET Framework规定的标准一致。这个特性使得C++/CX、CLI语言和Javascript的代码可以互相引用。

运行库 (Runtime library)

C++/CX有一系列针对WinRT的代码库,帮助C++程序员将使用C++標準程式庫的代码移植到WinRT.

{{bottomLinkPreText}} {{bottomLinkText}}
C++/CX
Listen to this article

This browser is not supported by Wikiwand :(
Wikiwand requires a browser with modern capabilities in order to provide you with the best reading experience.
Please download and use one of the following browsers:

This article was just edited, click to reload
This article has been deleted on Wikipedia (Why?)

Back to homepage

Please click Add in the dialog above
Please click Allow in the top-left corner,
then click Install Now in the dialog
Please click Open in the download dialog,
then click Install
Please click the "Downloads" icon in the Safari toolbar, open the first download in the list,
then click Install
{{::$root.activation.text}}

Install Wikiwand

Install on Chrome Install on Firefox
Don't forget to rate us

Tell your friends about Wikiwand!

Gmail Facebook Twitter Link

Enjoying Wikiwand?

Tell your friends and spread the love:
Share on Gmail Share on Facebook Share on Twitter Share on Buffer

Our magic isn't perfect

You can help our automatic cover photo selection by reporting an unsuitable photo.

This photo is visually disturbing This photo is not a good choice

Thank you for helping!


Your input will affect cover photo selection, along with input from other users.

X

Get ready for Wikiwand 2.0 🎉! the new version arrives on September 1st! Don't want to wait?