C++/CLI Programming


1.1.   C++/CLI Programming

C++/CLI provides you with a number of extensions and additional capabilities whit regard to standard C++. First we will summarize additional capabilities before we go into details.
Additional capabilities of C++/CLI are:
·         All the ISO/IEC fundamental data types can be used in C++/CLI program.
·         C++/CLI provides you with its own mechanism for keyboard input and output to the command line in a console program,
·         C++/CLI introduces the safe_cast operator which will ensure that a cast operation results in verifiable code being generated,
·         C++/CLI provides you with alternative enumeration capability that is class based and offers more flexibility that the ISO/IEC C++ enum declaration you have seen

1.1.1.                 C++/CLI Specific: Fundamental data Types

You can and should use the ISO/IEC C++ fundamental data type names in your C++/CLI programs, and with arithmetic operations, they work exactly as you have seen in native C++. Although all the operations with fundamental types you have seen work in the same way in C++/CLI, the fundamental type names in a C++/CLI program have a different meaning and introduce additional capabilities in certain situations. A fundamental type in a C++/CLI program is a value class type and can behave either as an ordinary value or as an object if the circumstances require it. Within the C++/CLI language, each ISO/IEC fundamental type name maps to a value class type that is defined in the System namespace. Thus, in a C++/CLI program, the ISO/IEC fundamental type names are shorthand for the associated value class type. This enables the value of a fundamental type to be treated simply as a value or be automatically converted to an object of its associated value class type when necessary. The fundamental types, the memory they occupy, and the corresponding value class types are shown in the following table:

FUNDAMENTAL TYPE
SIZE(BYTES)
CLI VALUES VLASS
bool
1
System::Boolean
char
1
System::SByte
signed char
1
System::SByte
unsigned char
1
System::Byte
short
2
System::Int16
unsigned short
2
System::UInt16
int
4
System::Int32
unsigned int
4
System::UInt32
long
4
System::Int32
unsigned long
4
System::UInt32
long long
8
System::Int64
unsigned long long
8
System::UInt64
float
4
System::Single
double
8
System::Double
long double
8
System::Double
wchar_t
2
System::Char

By default, type char is equivalent to signed char , so the associated value class type is System:: SByte . Note that you can change the default for char to unsigned char by setting the compiler option /J , in which case, the associated value class type will be System::Byte . System is the root namespace name in which the C++/CLI value class types are defined. There are many other types defined within the. C++/CLI also defines the System::Decimal value class type within the System namespace, and variables of type Decimal store exact decimal values with 28 decimal digits precision. As I said, the value class type associated with each fundamental type name adds important additional capabilities for such variables in C++/CLI. When necessary, the compiler will arrange for automatic conversions from the original value to an object of a value class type, and vice versa; these processes are referred to as boxing and unboxing , respectively. This allows a variable of any of these types to behave as a simple value or as an object, depending on the circumstances.
Because the ISO/IEC C++ fundamental type names are aliases for the value class type names in a C++/CLI program, in principle, you can use either in your C++/CLI code. For example, you already know you can write statements creating integer and floating - point variables like this:
int count = 10;
double value = 2.5;
You could use the value class names that correspond with the fundamental type names and have the program compile without any problem, like this:
System::Int32 count = 10;
System::Double value = 2.5;
Note that this is not exactly the same as using the fundamental type names such as int and double in your code, rather than the value class names System::Int32 and System::Double. The reason is that the mapping between fundamental type names and value class types I have described applies to the Visual C++ 2010 compiler; other compilers are not obliged to implement the same mapping. Type long in Visual C++ 2010 maps to type Int32, but it is quite possible that it could map to type Int64 on some other implementation. On the other hand, the representations of the value class type that are equivalents to the fundamental native C++ types are fixed; for example, type System::Int32 will always be a 32 - bit signed integer on any C++/CLI implementation. Having data of the fundamental types being represented by objects of a value class type is an important feature of C++/CLI. In ISO/IEC C++, fundamental types and class types are quite different, whereas in C++/CLI, all data is stored as objects of a class type, either as a value class type or as a reference class type. Next, you’ll try a CLR console program.

1.2.   Discovering C++/CLI Types

The native typeid operator does not work with CLR reference types. However, C++/CLI has its own mechanism for discovering the type of an expression. For variables x and y, the expression (x*y).GetType() will produce an object of type System::Type that encapsulates the type of an expression. This will automatically be converted to a System::String object when you output it. For example:
int x = 0;
double y = 2.0;
Console::WriteLine(L"Type of x*y is {0}", (x*y).GetType());
Executing this fragment will result in the following output:
Type of x*y is System.Double
Of course, you could use the native typeid operator with the variables x and y and get a type_info object, but because C++/CLI represents a type as a System::Type object, I recommend that you stick to using GetType() . C++/CLI also has its own version of typeid that you can only apply to a single variable or a type name. You can write x::typeid to get the System::Type object encapsulating the type of x. You can also write String::typeid to get the System::Type object for System::String .
Share on Google Plus