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:

    —the main jet
    Struggling aloft unti it seems at rest

    In the act of rising, until
    The very wish of water is reversed,
    Richard Wilbur (b. 1921)

    There can only be one Commander-in-Chief. In these times, crises cannot be managed and wars cannot be waged by committee. To the ears of the world, the President speaks for the nation. While he is of course ultimately accountable to Congress, the courts, and the people, he and his emissaries must not be handicapped in advance in their relations with foreign governments as has sometimes happened in the past.
    Gerald R. Ford (b. 1913)