Google
 
   
Login
Username:

Password:


Lost Password?

Register now!
Search
Main Menu
top books
Polls
What do you think about php-deluxe.net?
Excellent!
Cool
Hmm..not bad
What the hell is this?
encyclopedia
recommendation
compare webbrowser
Freenet DSL
Who's Online
6 user(s) are online (6 user(s) are browsing encyclopedia)

Members: 0
Guests: 6

more...
browser tip
Unix Befehle
manual of unix befehle
recommendation!
Sponsored
partner

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 ==

  • Programmers need not worry about manual memory management; any classes under Managed C++ can be designated to be managed by the CLR Garbage collection (computer science) simply by adding a modifier keyword, __gc before the class keyword. i.e. __gc class new { }; is managed under the garbage collector.
  • Managed and unmanaged code can be mixed together in the same assembly seamlessly. This allows the programmer to keep unmanaged code that cannot be ported over to the .NET Framework without re-writing it completely. Some ramifications of using this hybrid convention are present though.
  • Managed C++ is the only language able to natively communicate with all other .NET languages and native unmanaged C++ code. Managed C++ is thus very convenient for interoperability between programmers who use different languages, including those in the .NET theatre and those who use unmanaged C++.
  • == Disadvantages of using Managed C++ over unmanaged code ==

  • Managed C++ introduces a lot of new keywords and syntactic conventions that can impede on the readability of managed code, especially if C++ code is included directly and interacts directly with Managed C++ code in the same assembly.
  • Like in Java programming language, there is the presence of an unnecessary overhead of features that most Managed C++ programmers and programs don t really need. This will reduce performance on most applications. The overhead of a garbage collector is the main performance inhibitor. In almost all performance-critical applications, unmanaged native code is the preferred option.
  • Managed C++ is to be superseded by C_Plus_Plus/CLI and thus is to be made obsolete when C++/CLI is released as a final standard.
  • Managed C++ does not support ASP.NET web applications, which is a capability supported by all languages targeting the .NET Framework, including other third party languages.
  • Managed C++ requires a slightly longer development time than other .NET languages that could be applied to projects that still produce the same result, since the implications of pointers in C++ are still required, even in managed C++ code.
  • Managed C++ is a language only readily usable by programmers already familiar with C plus plus or another derived language. Visual Basic programmers, for example, will have trouble implementing Managed C++ in their solutions, while C# programmers, though they may have some difficulty, can adopt to Managed C++ quicker.
  • Managed C++ includes no support for generic programming. C_Plus_Plus/CLI is to support this though.
  • = 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++.

  • (Global change) Existing C++ to be ported over the CLR must be appended with the following:
  • //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.

  • 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 Common Intermediate Language.

  • A class can be designated to be garbage collected via the __gc extension keyword.
  • //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.

  • An interface can be declared with the __gc extension keyword preceding it. Such as:
  • //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

  • Java is supported on almost all platforms, while Managed C++ applications can only run on Windows or other implementations of the .NET Framework.
  • Java code is less complex and readability is much more fluent in Java code. Java also provides documentation on the source code, while Managed C++ does not. (C++/CLI will support this feature in Visual C++ .NET 2005)
  • Java has many other development tools and solutions available for Java programmers to use, while Managed C++ is only available under Visual Studio .NET. Managed C++ applications can be compiled using the Visual C++ Toolkit 2003 however, which is provided free of charge .
  • Exceptions in Java are checked, while in Managed C++ they are unchecked. (This can be considered an advantage over other languages depending on the code)
  • Advantages

  • Managed C++ can access the computer system on a low level interface much more efficient than Java. Java programmers must use the JNI (Java Native Interface) to utilise low level services of the host operating system. (This applies to Windows only)
  • Exceptions in Managed C++ are unchecked, while in Java they are checked. (This can be considered an advantage over other languages depending on the code)
  • === ...to C# ===

    Disadvantages

  • Like Java, C# is much less complex. And C# supports the .NET Framework natively.
  • While C# supports pointers just as in C++, this feature is turned off by default.
  • C# can achieve basically the same result when applied to the same solution as one used with Managed C++, as all syntactic and structural conventions remain strikingly similar.
  • C# is a much more strongly typed language, which helps reduce certain bugs, such as buffer overflow errors.
  • Managed C++, while also is a strongly typed language due to its introduction into the CLR, can be prone to errors if unmanaged native code is introduced in the same solution, while C# is pure MSIL.
  • Advantages

  • C# must use the .NET Framework and provided class libraries to access the computer system on a low level.
  • Using Managed C++ to port over applications to the .NET Framework from C or C++ is much easier to do using Managed C++.
  • The Microsoft Visual C++ .NET compiler, which compiles Managed C++ to target the .NET Framework, produces a much more matured set of instructions in its resultant assembly, thus improving performance. Performance will vary depending on the code, but in general, Managed C++ code (MSIL) is slightly faster or more efficient than code (MSIL) compiled using the C# compiler.
  • === ...to unmanaged C++ ===

    Disadvantages

  • Native C++ code is by far, much faster at runtime.
  • Native C++ supports generic programming. Until the final release of C++/CLI however, Managed C++ programmers must revert for workarounds for using generics in their solutions.
  • Native C++ supports the keyword const and const correctness. Managed C++, like Java and C#, does not contain this feature.
  • Native C++ code is not constricted by the CLR s restrictions. For example, the CLR does not allow classes to inherit other classes privately, thus
  • 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

  • Managed C++ supports Reflection (computer science), which is generally much more convenient depending on the function of the code, or what the code is intended for.
  • Managed C++ does not require manual memory management.
  • Managed C++ can interoperate with all other .NET capable languages, including other third party languages.
  • = References and external links =

  • [http://msdn.microsoft.com/library/default.aspurl=/library/en-us/vcmxspec/html/vcmanagedextensionsspec_start.asp Managed Extensions for C++ (at MSDN)]
  • = See also =

  • [http://en.wikipedia.org/wiki/Category:Programming_language_comparison Comparison of different Programming Languages]