Main Programmatic Changes in Managed C++
The following list of changes pertain to the differences in Object Oriented Programming compared to programming with standard C++.
- (Global change) Existing C++ to be ported over the CLR must be appended with the following:
A new preprocessor directive
#usingis 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
#usingand
using namespace System::Windows::Forms;to utilize Windows Forms.
- To compile code to target the CLR, a new compiler option must be introduced.
/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.
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. To achieve the same with unmanaged code, the delete
keyword is required:
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
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:
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.
Read more about this topic: Managed Extensions For C++
Famous quotes containing the words main and/or managed:
“My main wish is to get my books into other peoples rooms, and to keep other peoples books out of mine.”
—Samuel Butler (18351902)
“Its not greed and ambition that makes warsits goodness. Wars are always fought for the best of reasons, for liberation or manifest destiny, always against tyranny and always in the best interests of humanity. So far this war, weve managed to butcher some 10,000,000 people in the interest of humanity. The next war, it seems well have to destroy all of man in order to preserve his damn dignity.”
—Paddy Chayefsky (19231981)