Managed Extensions for C++ (简体)
C++托管擴展(Managed Extensions for C++)是對C++的一個屬性和關鍵字的擴展,以便於在微軟公司的.NET Framework進行編程。它也經常被稱為托管C++。
注意:C++托管擴展正在被一個新的語言規範,正在標準化的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語句才被支持。
目錄 |
以下列出物件導向程式設計與 unmanaged C++ 之間的差異性。
//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;
to utilize Windows Forms.
cl.exe hello.cpp /clr
/clr enables any code referencing the .NET Framework to be compiled as CIL.
__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;
}
The preceding code can be compiled and executed without any fear of memory leaks. Because class gc is managed under the garbage collector, there is no need to call the delete operator. Thus this program can run for an indefinite period of time without wasting any memory. To achieve the same with unmanaged code, the delete keyword is required:
//nogc.cpp
class gc {
int* i;
char* g;
float* j;
};
int main() {
while(true) {
gc* _gc = new gc();
delete _gc;
}
return 0;
}
NOTES:
A __gc designated class can have a constructor declared.
A __gc designated class can have a destructor declared.
A __gc designated class cannot inherit more than one class. (This is a limitation of the CLR)
A __gc designated class cannot inherit another class that is not __gc designated.
A __gc designated class cannot be inherited by another class that is not __gc designated.
A __gc designated class can implement any number of __gc interfaces.
A __gc designated class cannot implement an unmanaged interface.
A __gc designated class is by default not made visible outside of its own assembly. Use
public __gc class hey { };
the public keyword to modify the access of the a __gc designated class.
A __gc designated class can be destroyed manually using the delete keyword, but only if the __gc designated class has a user-defined destructor.
//interface.cpp
#using <mscorlib.dll>
__gc __interface ClassBase {
void Init();
int Common();
}
The preceding code must be compiled with /clr and /LD to produce a simple DLL file.
NOTES:
A __gc __interface cannot contain any data members, static members, nested class declarations and no access specifiers.
A __gc __interface can only inherit from another __gc __interface interface or the System::Object. Inheritance from System::Object is the default behavior.
A __gc __interface cannot contain any implementation (body code) of its declared function prototypes.
以下列出 Managed C++ 與其它程式語言在類似觀念上的差異。
缺點
優點
缺點
優點
Disadvantages
public __gc class one { int i; };
public __gc class two: private one { int h; i = h; }; //error
will produce a compiler error.
[Though this is not necessarily a redeeming feature, as the point of making a class private is to prevent inheritance or access outside the class library.]
Also, __gc classes cannot inherit from more than one class, as such
__gc class a {};
__gc class b {};
__gc class c: public a, public b {}; //will produce an error
the preceding will produce a compile error.
[Is this an advantage when multiple derivation leads to problems. It could be interpreted as an advantage of managed code to prohibit poor technique.]
Advantages
|
檢 • 論 • 編 • 歷
|
|
|---|---|
| 架構 | Base Class Library · Common Language Infrastructure · .NET assembly · .NET metadata |
| 共同語言基礎 | Common Language Runtime · Common Type System · Common Intermediate Language · Virtual Execution System · Dynamic Language Runtime |
| Languages | C# · Cω · Visual Basic .NET · C++/CLI (Managed) · J# · JScript .NET · Windows PowerShell · IronPython · IronRuby · F# · Nemerle · BOO |
| 視窗基礎: | Presentation · Communication · Workflow |
| 相關: | ADO.NET · ASP.NET · .NET Remoting · LINQ · Silverlight · CardSpace · Windows Forms · XAML |
| 其它應用: | Mono · .NET Compact Framework · .NET Micro Framework · Shared Source CLI · DotGNU · Portable.NET |
| 比較: | C# vs. Java · C# vs. VB.NET · Comparison of the Java and .NET platforms |
Why are we here?
All text is available under the terms of the GNU Free Documentation License
This page is cache of Wikipedia. History