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


 

 

Early Stages of the C++ .Net 23

(From Managed Extensions for C++ to C++ .NET)

 

 

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 dominated by Visual C++ .Net 2003 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 page.

  1. Managed Extensions for C++: __value keyword

---------------------Next-----------------------------

  1. Managed Extensions for C++: __nogc keyword

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

  3. Managed Extensions for C++: __interface keyword

  4. Managed Extensions for C++: __sealed keyword

  5. The new C++ sealed keyword

  6. The new C++ abstract keyword

  7. The new C++ override keyword

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

  9. The new C++ novtable keyword

  10. The Managed Extensions for C++: __gc keyword

  11. The __event keyword

 

  1. Native Events

  2. COM Events

  3. Managed Events

  4. Example: Native Events

  5. Example: COM Events

  6. Example: Managed Events

  7. Managed Extensions for C++: __delegate Keyword

  8. The new delegate Keyword

  9. Event Handling

  10. The __hook Keyword

  11. The __unhook Keyword

  12. The __raise Keyword

  13. Managed Extensions for C++: __box Keyword

  14. Implicit Boxing

  15. The C++ main() or Program Startup

  16. The Argument Definitions

  17. 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.

  1. __value class-specifier

  2. __value struct-specifier

  3. __nogc array-specifier

  4. __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:

  1. The __value keyword cannot be applied to an interface.

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

  3. A __value type is sealed (__sealed) by definition.

  4. 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:

  1. The value type must have LayoutSequential or LayoutExplicit.

  2. The value type can not have gc pointer members.

  3. 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 >