<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="zh">
	<id>https://arolstar52-zhtest.hf.space/index.php?action=history&amp;feed=atom&amp;title=C%2B%2B%2FCX</id>
	<title>C++/CX - 版本历史</title>
	<link rel="self" type="application/atom+xml" href="https://arolstar52-zhtest.hf.space/index.php?action=history&amp;feed=atom&amp;title=C%2B%2B%2FCX"/>
	<link rel="alternate" type="text/html" href="https://arolstar52-zhtest.hf.space/index.php?title=C%2B%2B/CX&amp;action=history"/>
	<updated>2026-07-20T01:37:54Z</updated>
	<subtitle>本wiki上该页面的版本历史</subtitle>
	<generator>MediaWiki 1.43.9</generator>
	<entry>
		<id>https://arolstar52-zhtest.hf.space/index.php?title=C%2B%2B/CX&amp;diff=1992133&amp;oldid=prev</id>
		<title>imported&gt;InternetArchiveBot：​补救2个来源，并将0个来源标记为失效。) #IABot (v2.0.8</title>
		<link rel="alternate" type="text/html" href="https://arolstar52-zhtest.hf.space/index.php?title=C%2B%2B/CX&amp;diff=1992133&amp;oldid=prev"/>
		<updated>2021-02-05T08:44:37Z</updated>

		<summary type="html">&lt;p&gt;补救2个来源，并将0个来源标记为失效。) #IABot (v2.0.8&lt;/p&gt;
&lt;p&gt;&lt;b&gt;新页面&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{unreferenced|time=2014-09-23T09:06:17+00:00}}&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;C++/CX&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;(Visual C++ Component Extensions，Visual C++ 组件扩展)&amp;#039;&amp;#039; 是微软的[[C++]][[编译器]]对C++的扩展，使得程序员可以比较方便地编写面向[[Windows Runtime]](&amp;#039;&amp;#039;WinRT&amp;#039;&amp;#039;)的程序。这个语言规范引入了一系列语法和类库抽象，以对C++程序员来说比较自然的方式暴露了基于[[组件对象模型]]（COM）的WinRT编程范型的接口。&lt;br /&gt;
&lt;br /&gt;
这个语言扩展从[[C++/CLI]]借用了语法，但是是面向WinRT和[[机器语言|原生码]]而不是[[通用语言运行库]]和[[托管代码]]。&lt;br /&gt;
&lt;br /&gt;
== 扩展语法 ==&lt;br /&gt;
C++/CX为WinRT编程引入了新的语法。和平台独立的语法和[[C++11]]标准兼容。&lt;br /&gt;
=== 物件 (Objects) ===&lt;br /&gt;
WinRT物件的创建方式是通过&amp;lt;code&amp;gt;ref new&amp;lt;/code&amp;gt;关键字，并赋值给具有&amp;lt;code&amp;gt;^&amp;lt;/code&amp;gt;(帽子)注记的类型的变量，这个帽子标记是继承自C++/CLI。&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
Foo^ foo = ref new Foo();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
一个WinRT变量仅仅是一对指针，一个指向[[虚函数表]]([https://en.wikipedia.org/wiki/Virtual_method_table Virtual method table]{{Wayback|url=https://en.wikipedia.org/wiki/Virtual_method_table |date=20150210070233 }})，另一个指向物件的[[不透明指针|内部数据]]([https://en.wikipedia.org/wiki/Opaque_pointer Opaque pointer]{{Wayback|url=https://en.wikipedia.org/wiki/Opaque_pointer |date=20150816200426 }})。&lt;br /&gt;
&lt;br /&gt;
==== 引用计数 (Reference counting) ====&lt;br /&gt;
&lt;br /&gt;
一个WinRT物件是使用[[引用计数]]模式来维护其生命期的，处理方式类似于被[[智能指针]]封装的常规C++物件。物件在没有引用时销毁。尽管没有[[垃圾回收]]机制，但是语言规范也保留了gcnew关键字以备以后使用。&lt;br /&gt;
&lt;br /&gt;
==== 類型 (Classes) ====&lt;br /&gt;
===== 運行類型 (Runtime classes) =====&lt;br /&gt;
運行類型包含了元件擴展的建構函式。這類型也被簡單地稱為 ref classes，因為它們是用 &amp;lt;code&amp;gt;ref class&amp;lt;/code&amp;gt; 創建的。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
public ref class MyClass&lt;br /&gt;
{&lt;br /&gt;
&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== 局部类型 (Partial classes) =====&lt;br /&gt;
&lt;br /&gt;
C++/CX引入了局部类型的概念。这个特性使得一个类的定义可以被分离到多个文件，使得[[XAML]][[图形用户界面]]设计工具能够自动生成部分代码到一个单独的文件，以避免干扰程序员写的代码。这些局部类型在编译时合并。这个特性在类似[[C♯|C#]]之类的CLI语言中已经存在很多年。局部类型并未成为C++标准，所以并不能在纯C++11代码中使用。&lt;br /&gt;
&lt;br /&gt;
这里是一个设计工具生成的局部类型示例：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// foo.private.h&lt;br /&gt;
#pragma once&lt;br /&gt;
&lt;br /&gt;
partial ref class foo&lt;br /&gt;
{&lt;br /&gt;
private:&lt;br /&gt;
   int id_;&lt;br /&gt;
   Platform::String^ name_;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
这里是一个程序员编写的类示例。生成的局部类型在开头被导入，注意这里&amp;lt;code&amp;gt;partial&amp;lt;/code&amp;gt;关键字不是必要的：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// foo.public.h&lt;br /&gt;
#pragma once&lt;br /&gt;
#include &amp;quot;foo.private.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
ref class foo&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
   int GetId();&lt;br /&gt;
   Platform::String^ GetName();&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
这是局部类型的实现部分代码的示例：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// foo.cpp&lt;br /&gt;
#include &amp;quot;pch.h&amp;quot;&lt;br /&gt;
#include &amp;quot;foo.public.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
int foo::GetId() {return id_;}&lt;br /&gt;
Platform::String^ foo::GetName {return name_;}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== 泛型 (Generics) ====&lt;br /&gt;
&lt;br /&gt;
WinRT支持[[泛型]]，所以C++/CX也支持。泛型类的信息被保留在元数据内，在运行时才实例化，而不是像[[C++模板]]那样在编译时示例化。编译器支持在一个类型中同时使用泛型和模板。&lt;br /&gt;
&lt;br /&gt;
泛型类示例：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
generic&amp;lt;typename T&amp;gt; &lt;br /&gt;
&lt;br /&gt;
public ref class bag &lt;br /&gt;
{&lt;br /&gt;
     property T Item;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 元数据 (Metadata) ==&lt;br /&gt;
&lt;br /&gt;
{{See also|CLI元数据}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
所有WinRT程序都通过[[元数据]]来描述其中的类型。元数据的格式和[[通用语言架构]](CLI)为[[.NET Framework]]规定的标准一致。这个特性使得C++/CX、[[CLI语言]]和Javascript的代码可以互相引用。&lt;br /&gt;
&lt;br /&gt;
== 运行库 (Runtime library) ==&lt;br /&gt;
&lt;br /&gt;
C++/CX有一系列针对WinRT的代码库，帮助C++程序员将使用[[C++標準程式庫]]的代码移植到WinRT.&lt;br /&gt;
&lt;br /&gt;
{{C语言}}&lt;br /&gt;
&lt;br /&gt;
[[Category:C++]]&lt;/div&gt;</summary>
		<author><name>imported&gt;InternetArchiveBot</name></author>
	</entry>
</feed>