For faster navigation, this Iframe is preloading the Wikiwand page for C++托管扩展.

C++托管扩展

C++托管扩展(Managed Extensions for C++)是对C++的一个属性和关键字的扩展,以便于在微软公司.NET Framework进行编程。它也经常被称为托管C++。2002年由微软发布。

注意:C++托管扩展自Visual C++ 2005起被一个新的语言规范,正在标准化的C++/CLI所取代。

托管C++并非独立存在的编程语言,而仅仅是微软对C++的一个语法扩展,允许C++程序员在.NET框架和CLR的基础上进行托管编程。与C#Visual Basic .NET相比,其主要优点是旧代码可以比较快地移植到新的平台上,而且即使不完全重写代码,也可以通过互操作在同一个模块中无缝整合托管和非托管代码,从新的.Net框架中获益。

.Net框架封装了大量的API,例如网络访问、字符串操作、数据访问、XML服务、图形界面控件库、邮件服务、加密服务、文件输入/输出,甚至是WMI管理,也使得应用程序员可以编写更加简洁的代码。目前只有托管C++及其后继者C++/CLI可以做到无缝整合托管和非托管代码,而在托管代码中调用COM的速度又相当慢,所以经常被用于其他语言和非托管代码之间的桥梁。

托管C++允许程序员编写托管代码,内存管理的工作现在可以让CLR去自动处理,访问时也增加了类型检查,减少了缓冲区溢出内存泄漏的危险,增加了程序的稳定性,但是在性能敏感的应用中,庞大的.NET框架和缓慢的自动内存管理并不是必要的,传统非托管代码仍然是一些人的首选。

在面向对象编程方面,主要的变化是对多重继承的限制,这是因为CLR的限制和内存管理的需要。一个托管类不能多继承基类。同时,类属性和微软中间语言(MSIL)的引入也使得托管类可以在其他语言中使用和继承。

与此同时,托管C++引入了大量的关键字和语义转换,减少了代码的可读性和明确性。缺少在很多语言中都支持的泛型和for each语句也增加了其他语言的程序员转向托管C++的困难。在其后继者C++/CLI中泛型和for each语句才被支持。

Managed C++ 的重大改變

以下列出物件導向程式設計與 unmanaged C++ 之間的差異性。

  • (Global change) Existing C++ to be ported over the CLR must be appended with the following:
//hello.cpp

//new using directive
#using <mscorlib.dll>

//another using namespace directive.
using namespace System;

int main()  {
  Console::WriteLine("Hello");
  return 0;
}

一個新的前置處理引導(preprocessor directive)

   #using <mscorlib.dll>

這是必須的。此外 #using directives 必須用 namespace 的方法來 import 更多的函式庫(libraries),像是 Base Class Library,例如:

  #using <System.Windows.Forms.dll>

以及

  using namespace System::Windows::Forms;

来使用 Windows Forms。

  • To compile code to target the CLR, a new compiler option must be introduced.
   cl.exe hello.cpp /clr

/clr enables any code referencing the .NET Framework to be compiled as CIL

  • A class can be designated to be garbage collected via the __gc extension keyword.
//gc.cpp

#using <mscorlib.dll>

__gc class gc  {
  int* i;
  char* g;
  float* j;
};

int main()  {
  while(true)  {
    gc* _gc = new gc();
  }
  return 0;
}

比較 Managed C++

以下列出 Managed C++ 與其它程式語言在類似觀念上的差異。

...to Java

缺點

  • Java 幾乎可支持所有的平台,而 Managed C++ 僅可以在 Windows 或其它實作出 .NET Framework 的平台上執行。
  • Java 程式碼複雜度較小,且易於閱讀,並提供有程式碼使用文件可供參考。而 Managed C++ 則無。 (C++/CLI 於 Visual C++ .NET 2005 中已有支援)
  • Java 有許多開發工具與解決方案可提供開發者利用,Managed C++ 僅能使用 Visual Studio .NET。不過,Managed C++ applications 可用免費的 Visual C++ Toolkit 2003 編譯。
  • Java 的例外機制是可以檢查的(checked), Managed C++ 則無法檢查。 (根据程序不同这也可能是个优点)

優點

  • Managed C++ 可以直接與系統低階(low level)服務介面溝通,Java 程式員必須使用 JNI (Java Native Interface) 與系統低階服務溝通。 (仅限Windows)
  • Managed C++ 是無法檢查例外,Java 可以檢查。. (根据程序不同这也可能是个缺点)

...to C#

缺點

  • 一如 Java, C# 的複雜度較小。而且 C# 可以直接支援 .NET Framework 。
  • C# 支援指標(pointers),如同 C++,這個機制預設是關掉的。



外部链接

{{bottomLinkPreText}} {{bottomLinkText}}
C++托管扩展
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?