1. What is managed C + +?
In answering this question, the first thing to figure out is "escrow" (Managed). Hosting is. NET, which is a new programming concept that melts into the common language runtime (CLR), so we can think of "managed" as ". NET". So what is the "common language runtime"? The common language runtime is the execution of a. NET Framework application. It provides a number of services, including: code management (Loading and execution), type security validation, metadata (advanced type information) access, managing memory for Management objects, managing code, interoperability of COM objects and prebuilt DLLs (non-Administrative Code and data), support for developer services, and more.
In other words, using managed C + + means that our code can be managed by the CLR and can be developed with the latest features such as automatic garbage collection, inter-program access, and so on. NET Framework application.
C + + Applications that are raised by managed concepts include managed code, managed data, and three components of a managed class.
(1) Managed code:. NET environment provides a number of core runtime services, such as exception handling and security policies. In order to be able to use these services, it is necessary to provide the runtime with some information code (metadata), which is managed code. All C #, vb.net, JScript.NET are managed by default, but Visual C + + is not managed by default, and command-line options (/clr) must be used in the compiler to produce managed code.
(2) Managed data: Managed data is closely related to managed code. Managed data is data that is allocated and freed by a garbage collector that is run by the common language. By default, C #, Visual Basic, and JScript.NET data are managed data. However, by using special keywords, C # data can be marked as unmanaged data. Visual C + + data is unmanaged by default, even when you use the/clr switch.
(3) Managed classes: Although Visual C + + data is unmanaged by default, you can use the "__gc" keyword to mark a class as a managed class when using Managed Extensions for C + +. As the name suggests, it indicates that the memory of the class instance is managed by the garbage collector. In addition, a managed class can be a member of the. NET framework, and the benefit is that it can interoperate correctly with classes written in other languages, such as managed C + + classes that can inherit from Visual Basic classes. But there are also limitations, such as a managed class can inherit from only one base class. It is necessary to note that you can use both managed and unmanaged classes in managed C + + applications. The unmanaged class here does not refer to a standard C + + class, but rather to a class that uses the __NOGC keyword in the managed C + + language.
2. Why use managed C + +
In addition to the full play. NET Framework, using managed C + + also has the following benefits:
(1) because in the same application, and even in the same file, we can use both managed C + + and traditional unmanaged C + + to write, we can take full advantage of the benefits of the two C + +, and can quickly port code and components to. NET Framework.
(2) Use hosting can be from any one. NET Framework-compatible language calls a C + + component or calls an unmanaged DLL, other libraries, classes, and so on.
(3) can be accessed directly from unmanaged code. NET Framework.
In summary, using managed C + + is written by C + + programmers. NET Framework application is one of the best options to fully understand. NET Framework, avoiding the use of other languages such as C #, VB. NET brings additional overhead.
Why use Managed Extensions for C + +
Microsoft should consider the compatibility of Visual C + + and make the traditional C + + language have enough ability to develop. NET application, the Managed Extensions for C + + are introduced in the new version of Visual C + +.
In the entire Visual Studio development suite, Microsoft was catered for. NET application development model, almost every tool has made a big or small improvement. Among them, the transformation of VB application of Microsoft software products the most. But too much change often leads to compatibility issues, especially in the new version of VB, which claims that it can only develop managed applications. NET application), so to the developer, this certainly means that the code written in the past VB in the new version of VB to reconstruct the difficulty will be very large. VB previously in version compatibility has unsatisfactory history-in VB4, VB5, VB6 between the upgrade, will make developers pay a lot of hard. Now, because the VB system has made a lot of changes, so version compatibility issues will be more serious-the author has seen some of Microsoft's official discussion groups of developers and related people expressed concern about this, And see more than one developer to the new version of VB in the compatibility of the problems raised doubts and even criticism.
As another "veteran" in the Microsoft Development Suite,--visual C + +, we provide complete. NET Development support is pleased with the same concern about its version compatibility issues--visual C + + is not as good as VB, completely split with MFC and ATL? The answer is: Impossible!
This is an exciting answer, let's take a moment to learn how the new version of Visual C + + deals with the relationship between change and backward compatibility.
In addition, Microsoft has made such a big reform of VB because Microsoft believes that VB is often applied to the fast application domain, which typically contains a database front-end application or a backend business component that is not very efficient. When the more excellent. NET Framework after the release, Microsoft for VB Exchange "Heart", in order to greatly enhance the function of VB, so that it becomes a rapid development. NET application is the main force. for Visual C + +, a tool that is still baodaobulao in many traditional areas, of course, you cannot be impatient and discard existing functionality. So, in the new version of Visual C + +, a much more eclectic approach-extending the existing C + + language-Allows Visual C + + to write purely. NET applications, you can still leverage their proven technology for the development of unmanaged applications.
Managed Extensions for C + + is a collection of extensions to the current C + + language that can help developers of Visual C + + write. NET Framework applications. Since the language has been extended rather than removed from the original C + + language, developers in Managed extensions can mix traditional unmanaged code with new, managed code in the same application. A direct benefit of this is that the application can enjoy unmanaged code attributes as well as managed code attributes. The same is true for component development, where traditional components can easily be packaged (wrapper) into. NET framework components, fully guaranteeing the investment of existing work.
In practice, using Managed Extensions is the best option for developers who experience the following development requirements:
You need to quickly merge unmanaged C + + applications into. NET Framework
For traditionally unmanaged C + + applications that were developed previously, because developers can mix two types of code in the same application, even in the same file, Managed Extensions provide a smooth way to transform the two code seamlessly.
Developers can continue to write components using unmanaged C + + to take advantage of the powerful features and flexibility of the language itself. Then, in order for the. NET Framework application to access this legacy component, developers can use Managed Extensions to write a small, highly efficient packaging (wrapper) program.
Need to make any kind of with. NET Framework-compatible languages can access C + + components
Managed Extensions supports invoking C + + classes from any. NET framework-compatible language. The invocation can be implemented because a managed extension can be used to write a simple wrapper class that exposes the corresponding C + + classes and methods to the Access party. These wrapper classes are managed and can be from other. NET Framework-compatible program. During invocation, the outsourced class acts as a mapping layer between the managed class and the unmanaged class-it lets the method invocation pass directly to the unmanaged class. In addition, it should be noted that Managed Extensions support calls to any unmanaged DLL or library.
Requires access in code that is never managed. NET Framework Class
To get more functionality, you can access classes in the. NET framework in unmanaged code. With Managed Extensions, you can create and invoke a. NET Framework class directly from your C + + code. In real-world programming, the handling of managed classes can be treated just like ordinary unmanaged C + + classes. In addition, call support for unmanaged COM is provided in the. NET Framework, and you can write unmanaged code for direct access.
Because managed code and unmanaged code each have advantages, in practical work, the developer can flexibly choose the use of both according to the actual situation of the project. Access to the COM interfaces provided by the. NET Framework provides better performance in some cases where access efficiency is pursued, while the ease with which the. NET Framework provides can make it easier for the developer to work with the simplicity that is provided in some cases where tasks need to be completed quickly.
Managed C + + is not a standalone programming language, but a syntax extension for C + + that allows C + + programmers to do so. NET Framework and the CLR for managed programming. With C # and visual Basic. NET, the main advantage is that the old code can be ported to the new platform relatively quickly, and even if the code is not completely rewritten, it is possible to seamlessly integrate managed and unmanaged code in the same module by interoperation, from the new one. NET Framework to benefit from: The NET Framework encapsulates a number of APIs, such as network access, string manipulation, data access, XML services, graphical interface control libraries, mail services, cryptographic Services, file input/output, and even WMI management, and enables application programmers to write more concise code. Currently only managed C + + and its successors C + +/CLI can seamlessly integrate managed and unmanaged code, and calling COM in managed code is quite slow, so it is often used as a bridge between other languages and unmanaged code.
Managed C + + allows programmers to write managed code, and memory management can now be used by the CLR to automate processing, while access also increases type checking, reduces the risk of buffer overruns and memory leaks, increases program stability, but is huge in performance-sensitive applications. NET Framework and slow automatic memory management are not necessary, traditional unmanaged code is still the first choice for some people.
3. What is the CLR
CLR common shorthand words, CLR is the common language runtime, Common Language runtime) and Java Virtual machine is also a runtime environment, it is responsible for resource management (memory allocation and garbage collection), and to ensure that the application and the underlying operating system between the necessary separation.
4. What is C + +/CLI
Http://baike.baidu.com/view/459502.htm
C + +/CLI Introduction http://blog.csdn.net/eric_jo/article/details/4184916
5. The main difference between managed C + + and standard C + +
Although managed C + + is built from standard C + +, it is fundamentally different from standard C + +, mainly in the following areas:
(1) Extensive use of "namespaces" (namespace)
Namespaces are a logical naming scheme for types. NET uses this naming scheme to group types by their logical categories of related features, and the use of namespaces makes it easier for developers to browse and reference types in code. Of course, we can also interpret namespaces as a "class library name."
Although Microsoft has supported namespaces programmatically in Visual C + + early on, it has rarely attracted the attention of Visual C + + programmers. Now in the managed C + + program, we have to use the #using and using keyword in this way. For example, the following simple program code is to output "Hello world" on the console:
- #using
- using namespace System;
- int main (void)
- {
- Console::WriteLine (S"Hello World");
- return 0;
- }
#using using namespace System; int main (void) { Console::WriteLine (S "Hello World"); return 0; }
In code, #using是用来将一个元数据文件输入到托管C + + programs, these files can be MSIL (Microsoft intermediate Language, Microsoft intermediate language) files that contain managed data and structures, such as DLLs, EXE, obj files, and so on. Mscorlib.dll is a core class library of the. NET Framework that contains the primary namespace System. The second line of code for the program "using namespace System;" Used to use the System namespace. System is the. NET Framework root namespace, which contains the most basic types, such as the system for input/output for data flow:: Io, and so on.
In the ongoing development of managed C + + programs, we will soon find that many types of references are used in front of the program with #using and using.
(2) changes in basic data types
We know that the data type of the standard C + + language is very rich. Managed C + + has a richer data type that includes not only data types in standard C + +, but also __int64 (64-bit integer), decimal (96-bit decimal number), string* (String type), and object* (object type). Table 1-1 lists their respective data types.
(picture lost, added later)
It is important to note that when a variable is defined by string and object, it is important to have an asterisk ("*"), but this variable is not a pointer variable, which is different from the meaning of standard C + +. For example, the code above can be changed to:
- #using
- using namespace System;
- int main (void)
- {
- string* Hello = S"Hello World";
- Console::WriteLine (hello);
- return 0;
- }
#using using namespace System; int main (void) {string* Hello = S "Hello World"; Console::WriteLine (hello); return 0; }
(3) New three managed C + + types: __gc class, __value class, and __gc interface
A __gc class or struct means that the life cycle of the class or structure is made up of. NET development platform automatic management and automatic garbage collection, users do not have to call Delete to delete. Defining a __gc class or struct is basically similar to a standard C + +, unlike adding __gc to a class or struct, such as the following code:
- __gc class G
- {
- Public :
- int k;
- int sum (int);
- };
- G::sum (int i)
- {
- return i* (i + 1)/2;
- }
- int main ()
- {
- G * g = new G;
- Console::WriteLine (G->sum (4)); //Result output
- return 0;
- }
__gc class g{public:int k; int sum (int);}; g::sum (int i) {return i* (i + 1)/2;} int main () {G * g = new G; Console::WriteLine (G->sum (4)); The result is output of 0 return; }
But be aware that:
A. A __gc class cannot inherit from an unmanaged class and cannot contain unmanaged classes derived from it. However, a __gc class can inherit from up to one managed class.
B. A __gc class cannot be defined as a friend class or contains a friend member function. The so-called friend function is used to allow external functions to access private and protected type members in a class.
C. A __gc class cannot declare or define and overload a new or delete operation and cannot contain declarations such as using.
The __value class is used to use small data items with short lifetimes, unlike the __gc class. The __gc class data is allocated in the CLR heap, while the __value class object is on the run stack or called NDP (. NET Developer Platform. NET Developer platform), thus avoiding the overhead of the garbage collector allocating and freeing space. A __value class can be declared as a local variable, a parameter and a return value, or it can be embedded in a __gc class or as a static variable or a variable allocated in the C + + heap. For example, the following code:
- #using
- using namespace System;
- __value struct V { int i;};
- __gc struct G {v V;}; //embedded in the __gc class
- V F (v V) { //define a global function whose value is stored in the run stack
- V.I + = 1; //Does not affect the value of the original parameter V
- return v; //Returns the value of the V structure type
- }
- int main (void)
- {
- V v1 = {10}; //Declare and initialize in the run stack
- V v2 = f (v1); //Call F function, at which I in V1 is 10, and I in V2
- G *PG = new G; //Allocate heap space for G instances
- Pg->v = v1; //PG V for I is ten
- PG->V.I + = v2.i; //PG V for i is 10+11=21
- Console::WriteLine (V1.I); //Output result is ten
- Console::WriteLine (V2.I); //Output result is one
- Console::WriteLine (PG->V.I); //output result is
- return 0;
- }
Managed C + +, CLI, CLR