Managed Extensions for C Plus Plus |
= Overview =
Managed Extensions for C++ is a set of keywords and attributes to bring the C plus plus syntax and language to the .NET Framework. It is commonly referred to as Managed C++.
NOTE: Managed C++ is being superseded by a new language set, designated C Plus Plus CLI, which as of early 2005, is currently in beta testing. The information provided in this article will eventually become obsolete.
Managed C++ is not a complete standalone, or fully fledged programming language, but it is merely a set of grammatical and syntactic extensions implemented by Microsoft to bring C++ to the .NET Framework (essentially, the CLR) and to allow for managed programming for C++ programmers.
= Scope of this article =
It is also advised to the reader that this article be interpreted merely as informational and not to provide a tutorial on Managed Extensions for C++. While conventions and syntax may be explained here, it will not provide a crash-course or anything like that on how to program using Managed C++. Also, as stated before, this article will eventually be superseded by the new standard, C Plus Plus CLI.
= Who would use Managed C++ =
One of the many things about Managed C++ is the question of its usability. Who would use Managed C++ when C Sharp and Visual Basic .NET are already very powerful languages to use for programming with the .NET Framework
The main purpose of Managed C++ was not only to provide C++ programmers a way to utilize their skills in C++ to access a rich and stable platform for Windows programming, but also to allow existing C++ projects and source code to be speedily ported over to the .NET Framework, should programmers require it.
Since Microsoft has already released plans to base its next generation operating system, Windows Vista, partly on managed code, it would be logical to assume that existing technologies that Microsoft implements itself would be ported and re-implemented to provide the same base design and accessibility as the technology changes. It can be assumed that Managed C++ was conceived solely based on that reason.
= Additional or amended functionality provided in Managed C++ =
Programs coded in Managed C++ provide additional functionality of the .NET Framework and the Common Language Runtime. Most notable of these is garbage collection, which relieves the programmer of manual memory management. The Garbage collection (computer science) (or GC) is handled by the CLR. Memory management is executed quite quickly, but for more performance critical applications, native, unmanaged code is most likely the preferred option.
Also, C++ has evolved much over time and most implementations of the language are object oriented. Managed C++ and the use of classes and class based objects remains prevalent like in Visual C++. The only major change to this in Managed C++ is that the capabilities of multiple inheritance are not supported. This is because of a limitation of the CLR and the functions of the garbage collector. A class managed under the garbage collector cannot inherit more than one class, further is explained in the proceeding sections.
== Advantages of using Managed C++ over unmanaged code ==
== Disadvantages of using Managed C++ over unmanaged code ==
= Main Programmatic Changes in Managed C++ =
The following list of changes pertain to the theatre of differences in Object Oriented Programming compared to programming with unmanaged C++.
//hello.cpp
//new using directive #using
//another using namespace directive. using namespace System;
int main() { Console::WriteLine(L Hello ); return 0; }
A new preprocessor directive
#using
is required. In addition to that, more #using directives are required to import more libraries to use more namespaces in the Base Class Library, such as
#using
and
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 Common Intermediate Language.
//gc.cpp
#using
__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
__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.
== Comparing Managed C++ ==
The following contains main points and programmatic standards that differ between Managed C++ and other well known programming languages that are similar in concept.
=== ...to Java ===
Disadvantages
Advantages
=== ...to C# ===
Disadvantages
Advantages
=== ...to unmanaged C++ ===
Disadvantages
public __gc class one { int i; }; public __gc class two: private one { int h; i = h; }; //error
will produce a compiler error.
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.
Advantages
= References and external links =
= See also =
|
|