1 / 9

.NET 语言

.NET 语言. C++ with Managed Extensions. C++ with Managed Extensions. 微软不能单方面修改 C++ 微软为 C++ 定义了一套受控扩充件 (Managed Extensions) Managed C++ 定义了一些新关键字 __gc: 指出某个数据类型受垃圾回收机制管制。 __value: 指出某个数据类型不受垃圾回收机制管制。 __interface: 定义一个 CTS 接口类型。 __box: 将 CTS 值类型转换成引用类型。 __unbox: 将装箱的 CTS 值类型转回其原来形式。

kaseem-rios
Télécharger la présentation

.NET 语言

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. .NET 语言 C++ with Managed Extensions

  2. C++ with Managed Extensions • 微软不能单方面修改C++ • 微软为C++定义了一套受控扩充件(Managed Extensions) • Managed C++定义了一些新关键字 • __gc:指出某个数据类型受垃圾回收机制管制。 • __value:指出某个数据类型不受垃圾回收机制管制。 • __interface:定义一个 CTS 接口类型。 • __box:将 CTS 值类型转换成引用类型。 • __unbox:将装箱的 CTS 值类型转回其原来形式。 • __delegate:定义一个 CTS 委派类型。

  3. Managed C++程序样例 • 例1. 用 C++ 托管扩展编写的 Hello World (HelloVC.cpp) #using <mscorlib.dll> // Allow easy reference to the System namespace classes. using namespace System; // The global function, main, is the application's entry point. void main() { // Write text to the console. Console::WriteLine(S"Hello World using Managed Extensions for C++!"); }

  4. Managed C++程序样例 • 用 C++编写托管目标代码,要求原程序中包含 #using <mscorlib.dll> 编译时加clr选项。 • 在 C++ 托管扩展中,#using 指令与 #import 指令类似,用于合并类型库中的信息。 • #using与 #include 指令有区别,#include 用于合并源代码而不是预生成库。 • 为将命名空间导入到程序中(换言之,为便于引用 System 对象而不必完全限定其路径),还需要加上下面这条语句: using namespace System;

  5. Managed C++程序样例 • 在 C++ 托管扩展中,Console::WriteLine 中需要有双冒号来表示范围。双冒号用于分隔命名空间和类名称以及类名称和静态方法。 • 字符串前面的 S 通知编译器将其编译为 System::String*,这在托管代码中的性能要好于 C++ 字符串。 • 使用以下命令行指令来编译此程序: cl.exe /clr HelloVC.cpp clr 开关,为公共语言运行库编译,它按照运行库的要求告知编译器创建托管代码。 • 最后,运行结果可执行程序将产生以下输出: Hello World using Managed Extensions for C++!

  6. Managed C++程序样例 • 例2 // A Managed C++ example #using <mscorlib.dll> __gc __interface IMath { int Factorial(int f); double SquareRoot(double s); };

  7. Managed C++程序样例 __gc class Compute : public IMath { public: int Factorial(int f) { int i; int result = 1; for (i=2; i<=f; i++) result = result * i; return result; } ; public: double SquareRoot(double s) { return System::Math::Sqrt(s); } } ;

  8. Managed C++程序样例 void main(void) { Compute *c = new Compute; int v; v = 5; System::Console::WriteLine( "{0} factorial: {1}", __box(v), __box(c->Factorial(v))); System::Console::WriteLine( "Square root of {0} : {1:f4}", __box(v), __box(c->SquareRoot(v))); }

  9. Managed C++程序样例 • Managed C++要求装箱必须显示进行。 • Managed C++代码和UnManaged C++代码可共存于同一个进程中。 • Managed C++对CLR提供了完整的支持。 • C++是VS.NET中唯一能直接编译为本机代码的语言。

More Related