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.
---------------------Next-----------------------------
| 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.
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 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:
In Managed Extensions for C++, the equivalents to a C# class and a C# struct are as follows:
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. | |||||||||

Figure 1.

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.

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.

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

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.

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

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

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.
