|< C++ .Net System Programming 8 | Main | Old To New C++ .Net version 2 >|


 

 

Early Stages of the (Visual) C++ .Net 24

(From Managed Extensions for C++ To new C++ Syntax)

 

 

 

Let have a break! In the previous modules, you have encountered many new keywords used that not available in native C++. In this module we try to understand those keywords, whether it is used in the the old version of the Managed Extension for C++ or new C++ .Net. At the same time we are starting to migrate from the Managed Extension for C++ to the full new C++ .Net. Do expect the mixed old and new code syntaxes and keywords. The code snippets used in this module if any are Visual C++ .Net 2003 dominated and if compiled using Visual C++ .Net 2005 you need to use the /clr:oldSyntax option). The following are all the topics available in this module.

 

Managed Extensions for C++: __value keyword

Managed Extensions for C++: __nogc keyword

Using the equivalent functionality in the new C++ syntax: Classes and Structs (Managed)

Managed Extensions for C++: __interface keyword

Managed Extensions for C++: __sealed keyword

The new C++ sealed keyword

The new C++ abstract keyword

The new C++ override keyword

The new C++ new (new slot in vtable) keyword

The new C++ novtable keyword

The Managed Extensions for C++: __gc keyword

The __event keyword

Native Events

COM Events

Managed Events

Example: Native Events

Example: COM Events

Example: Managed Events

Managed Extensions for C++: __delegate Keyword

The new delegate Keyword

Event Handling

The __hook Keyword

The __unhook Keyword

The __raise Keyword

Managed Extensions for C++: __box Keyword

Implicit Boxing

The C++ main() or Program Startup

The Argument Definitions

Customizing C++ Command-Line Processing

 

 

Managed Extensions for C++: __value keyword

 

This should applies only to version 1 of Managed Extensions for C++. This syntax should only be used to maintain version 1 code. The new equivalent syntax information provided in the next section. The __value keyword declares a class to be a __value type. The syntaxes are shown below.

 

__value class-specifier

__value struct-specifier

__nogc array-specifier

__nogc pointer-specifier

 

A __value type differs from __gc types in that __value type variables directly contain their data, whereas managed variables point to their data, which is stored on the common language runtime heap. The following conditions apply to __value types:

 

          The __value keyword cannot be applied to an interface.

          A __value type can inherit from any number of interfaces and cannot inherit from other types or __value types.

          A __value type is sealed (__sealed) by definition.

          It is valid to use a __value type anywhere a managed type is allowed.

 

The __value keyword is not allowed when used with the __abstract keyword. A __value type can be explicitly connected to a System::Object pointer. This is known as boxing. The following guidelines apply to embedding a value type inside a __nogc type:

 

          The value type must have LayoutSequential or LayoutExplicit.

          The value type can not have gc pointer members.

          The value type can not have private data members.

 

 

In Managed Extensions for C++, the equivalents to a C# class and a C# struct are as follows:

 

Managed Extensions for C++

C#

 

__gc struct or __gc class

 

class

__value struct or __value class

 

struct

 

Table 1

 

In the following example, a __value type (Val) is declared and then two instances of the __value type are manipulated. Firstly let compile the code using Visual C++ .Net 2003. Create a new .Net Console Application as shown below. Named it as Testprog. Your project is a classic Hello World example.

 

Visual C++ 2003 new project window

 

Figure 1.

 

Testprog.cpp main project file

 

Figure 2

 

Next, edit the main file (Testprog.cpp in this case) as shown below.

 

// This is the main project file for VC++ application project

// generated using an Application Wizard.

 

#include "stdafx.h"

#using <mscorlib.dll>

using namespace System;

 

__value struct Val

{

   int mem_i;

};

 

int _tmain()

{

   // TODO: Please replace the sample code below with your own.

   Val testval1, testval2;

 

   testval1.mem_i = 5;

   Console::Write(L"testval1.mem_i = ");

   Console::WriteLine(testval1.mem_i);

 

   testval2 = testval1;  // copies all fields of testval1 to testval2

   testval2.mem_i = 6;   // does not affect testval1.mem_i

 

   Console::Write(L"testval2.mem_i = ");

   Console::WriteLine(testval2.mem_i);

 

   return 0;

}

 

Build and run your program. The output is something like the following.

 

Console application output sample

 

Next we will try to compile the same code using Visual C++ .Net 2005. Create a new CLR Console Application as shown below. Name it as Testprog1.

 

Visual C++ 2005 Express Edition new project window

 

Figure 3

 

Edit the main() program as the previous code, shown below. Notice the main() arguments. It is an array template.

 

Testprog1.cpp the main project file - Visual C++ 2005 Express Edition

 

Figure 4

 

// Testprog1.cpp : main project file.

 

#include "stdafx.h"

#using <mscorlib.dll>

using namespace System;

 

__value struct Val

{

   int mem_i;

};

 

int main()

{

   Val testval1, testval2;

 

   testval1.mem_i = 5;

   Console::Write(L"testval1.mem_i = ");

   Console::WriteLine(testval1.mem_i);

 

   testval2 = testval1;  // copies all fields of testval1 to testval2

   testval2.mem_i = 6;   // does not affect testval1.mem_i

 

   Console::Write(L"testval2.mem_i = ");

   Console::WriteLine(testval2.mem_i);

 

   return 0;

}

 

Make sure you set your project property to /clr:oldSyntax as shown below.

 

The /clr:OldSyntax option setting in the project property page of VC++ 2005 EE

 

Figure 5

 

Build and run your project. The expected output is something like the following.

 

VC++ 2005 EE console application output sample

 

Finally, let use the new C++ syntax. Edit the main() program code as shown below and set your project to /clr option shown below.

 

Setting the /clr option through the project property page of VC++ 2005

 

Figure 6

 

// Testprog1.cpp : main project file.

 

#include "stdafx.h"

// #using <mscorlib.dll> // included by default

using namespace System;

 

value struct Val

{

   int mem_i;

};

 

int main(array<System::String ^> ^args)

{

    Val testval1, testval2;

 

   testval1.mem_i = 5;

   Console::Write("testval1.mem_i = ");

   Console::WriteLine(testval1.mem_i);

 

   testval2 = testval1;  // copies all fields of testval1 to testval2

   testval2.mem_i = 6;   // does not affect testval1.mem_i

 

   Console::Write("testval2.mem_i = ");

   Console::WriteLine(testval2.mem_i);

 

    return 0;

}

 

Build and run your project. The output is shown below. Now, you should get the idea of compiling Managed Extension of C++ and using a new C++ syntax.

 

Console application program output example

 

---------------------------------------------------------------------

 

 

 

Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7 | Part 8 | Part 9

 

|< C++ .Net System Programming 8 | Main | Old To New C++ .Net version 2 >|