Managed Extensions For C++ - Main Programmatic Changes in Managed C++

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:
//hello.cpp //new using directive #using //another using namespace directive. using namespace System; int main { Console::WriteLine("Hello, world!"); 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 CIL.

  • 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 = gcnew 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. 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.

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 people’s rooms, and to keep other people’s books out of mine.
    Samuel Butler (1835–1902)

    It’s not greed and ambition that makes wars—it’s 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, we’ve managed to butcher some 10,000,000 people in the interest of humanity. The next war, it seems we’ll have to destroy all of man in order to preserve his damn dignity.
    Paddy Chayefsky (1923–1981)