C ++/CLI Learning

Source: Internet
Author: User

Each time we assign the same object with a value type, a new boxing of the value occurs.
Allowing access to the boxed Value Type allows in-memory update, which may provide significant
Performance...
Each time we allocate an object of the same value type, a packing operation is performed. The allowed access to the boxed value type in the memory can be significantly improvedProgramPerformance.

A form of Deep-copy semantics associated with the c ++ copy constructor and copy assignment
Operator; however, this cocould not be extended to value types.
Associate the deep copy syntax with the copy constructor and copy operator of C ++, but it does not extend to the value type.

The CLI has no notion of the class destructor for a reference type. So the Destructor has to be
Mapped into something else in the underlying implementation.
There is no destructor for reference types in CLI. Therefore, the Destructor must be mapped to other functions during implementation.

We still need a way to automate the invocation of
Destructor. A special stack-based notation for a reference type is supported; that is, one in which its
Lifetime is associated within the scope of its declaration. Internally, the compiler transforms
Notation to allocate the reference object on the managed heap. With the termination of the scope,
The compiler inserts an invocation of the dispose () method-the User-Defined destructor. Reclamation
Of the actual memory associated with the object remains under the control of the garbage collector.
We still need a method to automatically call the destructor. A special stack flag is provided for the reference type. The object lifecycle is associated with the declared Code .
A universal compiler allocates a reference object to the managed stack based on this flag. At the end of the code snippet, the compiler inserts a dispose () call-User-Defined destructor.
The memory associated with the object to be recycled is still under the control of the garbage collector.
Ref class wrapper {
Native * PN;
Public:
// Structure is initialized
Wrapper (INT Val) {Pn = new Native (VAL );}

// Release the unmanaged memory by yourself
~ Wrapper () {Delete PN ;}

Void mfunc ();
Protected:
// Explicitly declare the Finalize method to ensure that the memory is released
! Wrapper () {Delete PN ;}
};
Void F1 ()
{
// Use a normal reference type
Wrapper ^ W1 = gcnew wrapper (1024 );

// Ing the reference type to the lifecycle
Wrapper W2 (2048 );

// Compare different call syntaxes
W1-> mfunc (); w2.mfunc ();
// W2 is released by dispose () here
}

//
//... Then, W1 may be released by finalize ().

four compiling modes of C ++/CLI
1. mixed Mode: Source-level mix of native and CTS types plus binary mix of native and cel
object files. (compiler switch: \ CLR .)
mixed mode: native types and CTS types are mixed at the code level and binary file level by using the \ CLR parameter
2. pure mode: Source-level mix of native and CTS types. all compiled to Cel object files.
(compiler switch: \ CLR: Pure .)
pure mode: the native type and CTS type are mixed at the code level, and all files are compiled to the template object file. The \ CLR: Pure parameter is used.
3. native class can Ho Ld cts types through a special Wrapper class only
the native class can only process CTS types using one special encapsulation class
4.cts classes can hold native types only as pointers
cts class can process only the pointer type of the native type.

Composition of C ++/CLI
1. assemblies (assemblies)
The assembler is the core of Building. Net distributed applications. An assembly is a set of self-descriptions stored in an intermediate language and is responsible for allocating the resources required by the application. An assembly consists of four parts: Assembly metadata, type metadata, Microsoft intermediate language code, and resources.
Private assemblies reside in the same directory as the application itself or in one of its child directories. Shared assemblies, on the other hand, are stored in the Global Assembly Cache (GAC ).
The private assembler is located in the same directory of the application or its subdirectory. The shared assemblies are stored in the Global Assembly Cache (GAC ).
Metadata Information in the assemblies can be obtained through reflection technology.
2. Common Language Runtime (Unified Language Runtime Environment)

As Managed Objects in. Net do not have a fixed location, but you can overcome this with the pin_ptr <> keyword.
The hosted object does not have a fixed address in. net, but you can use the pin_ptr <> keyword to control it.

The CLS is a minimum subset of the CTS that all ages must support to be.
CLS is the smallest subset of CTS and must be supported by all. NET languages.

• Global methods and variables are not allowed.
global functions and variables are not allowed
• the CLS does not impose case sensitivity, so make sure that all exposed types differ by more than their case.
CLS is case insensitive
• The only primitive types allowed are byte, int16, int32, int64, single, double, Boolean, Char, decimal, intptr, and string.
CLS only contains the preceding simple data types
• variable-length argument lists are not allowed. use fixed-length arrays instead.
use a fixed array instead of a Variable Parameter List
• pointers are not allowed.
pointer not allowed
• class types must inherit from a CLS-compliant class. system: object is CLS compliant.
the class type must inherit from the CLS system class (that is why all.. Net classes all inherit the value system: Object reason)
• array elements must be CLS compliant.
the array element must be an element in the CLS System

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.