C++/CLI can be said to be a new "dialect" of the standard C + + language, developed by Microsoft to take full advantage of the CLI (Common Language infrastructure) platform. So, what is its originality in language, below, let us start the wonderful C++/CLI language tour (all sample code in this article, all of them are compiled with Visual Studio.NET professional, all the explanations are also in visual Studio.NET 2005 environment based).
Assembly and metadata
The traditional C + + compilation pattern involves compiling a separate source file into the target file (obj) and linking the target file to the library function to generate an executable program. The CLI pattern is very different, and it involves the creation and use of assemblies.
Simply put, the assembly is a single compiled output, regardless of the number of input source files. If the output comes with an entry-point function (for example, the main function), it is an. exe file, and if not, it is a. dll file. Any compilation that is generated by referencing an external assembly must have access to the dependent assembly, and there is no header file mechanism to use for the traditional link, but rather to access the required external information through the compiler, looking inside the dependent assembly.
The assembly contains metadata that describes the type and function contained therein, as well as the CIL (Common intermediate Language) instruction--microsoft called "MSIL". Metadata and directives can be executed through the independent Ves (Virtual Execution System).
CLI type
Example 1 is a class that simulates a two-dimensional point. The namespace has to be mentioned here, all of the CLI standard library types belong to the System namespace, or are nested under a namespace within them, such as System::Object and System::String, and System::IO, System:: Text, System::runtime::compileroptions and so on. Tag 1 avoids the use of namespace qualifiers throughout your program.
Example 1:
/*1*/
using namespace System;
/*2*/
Public ref class Point
{
int x;
int y;
Public
Defines the X and Y instance properties for reading and writing
/*3a*/Property int X
{
/*3b*/int Get () {return x;}
/*3c*/void Set (int val) {x = val;}
}
/*4a*/Property int Y
{
/*4b*/int Get () {return y;}
/*4c*/void Set (int val) {y = val;}
}
Defining an instance constructor
/*5a*/Point ()
{
/*5b*/X = 0;
/*5c*/Y = 0;
}
/*6a*/Point (int xor, int yor)
{
/*6b*/X = xor;
/*6c*/Y = yor;
}
Defining instance methods
/*7a*/void Move (int xor, int yor)
{
/*7b*/X = xor;
/*7c*/Y = yor;
}
/*8a*/virtual bool Equals (object^ obj) override
{
/*8b*/if (obj = nullptr)
{
return false;
}
/*8c*/if (this = = obj)//Are we testing ourselves?
{
return true;
}
/*8d*/if (GetType () = = Obj->gettype ())
{
/*8e*/point^ p = static_cast<point^> (obj);
/*8f*/return (X = = p->x) && (Y = = p->y);
}
return false;
}
/*9*/virtual int GetHashCode () override
{
return X ^ (Y << 1);
}
/*10a*/virtual string^ ToString () override
{
/*10b*/return String::Concat ("(", X, ",", Y, ")");
}
};
In tag 2, we define a reference class called point (ref Class), a reference class that is a CLI reference type, and when used together, ref and class (with spaces between them) represent a new keyword.