|
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