C ++

xiaoxiao2021-03-06  25

8.2.1 Basic Type and CLI

Each base type is an abbreviation for the CLI to provide types. For example, keyword int is equivalent to value type system :: INT32. As a style, it is more inclined to use keywords instead of using a complete system type name.

The following table lists the basic type and the type provided by the CLI: This correspondence is still in the discussion, and it is still completely fixed now! (Translator Note: There is a corresponding table that represents the type of fundamental type and CLI, but this table is not given due to the text format and the relationship mentioned above. The corresponding relationship is obvious, and there is no Do not affect readers at all, understand the following.)

8.2.2 Transformation

Some new transformations are defined. It includes transformation of handle and argument arrays.

8.2.3 Array Type

An array in a C / CLI is different from the native array, the former is assigned in the CLI heap. Moreover, there may be no one order (RANK). This order determines the number of indexes of each array element. An array is also known as the array of dimensions. A first-order array is called a one-dimensional array. More than a first-order array is called a multi-dimensional array.

In this standard, array (array) is used to refer to an array of CLI. A C array is called native array. No matter when, it is necessary to distinguish it.

8.2.4 Type System Unified

C / CLI provides a "unified type system", all values ​​and handle types are exported for type System :: Object.

Calling an instance function on any value is possible, even if it is a value such as the foundation of INT.

This example

Int main () {

Console :: WriteLine ((3) .tostring ());

}

From the type system :: INT32, the function TOSTRING is called for a whole type, and the result is output 3. (Note, 3 side of the increasing parentheses are not redundant, they are used to get marked "3" and ".", Not "3.").

This example

Int main () {

INT i = 123;

Object ^ o = i; // boxing

INT j = static_cast (o); // unboxing

}

More interesting, a integer value can be turned into system :: Object to return to INT. The boxing and unboxing are shown here. When a variable of a value type is turned into a handle type, an object box (Box) is assigned to place the value, and the value is copied in the box (Box). The unboxing is exactly, the object box (Box) is converted back to its original values, and the value is copied from the box and puts the appropriate storage.

This type of system uniformly provides value type as an object of the object without introducing unnecessary overhead. For programs that do not need INT values, INT values ​​are 32-bit values. But for programs that require INT icons, this ability can be obtained as long as needed. This ability to make value types as objects fill the gap between value types in most languages. For example, a STACK class may provide a PUSH and POP function to return to the Object ^ value.

Public Ref class stack {public:

Object ^ POP () {...}

Void Push (Object ^ o) {...}

}

Because C / CLI has a unified type system, the Stack class can use any type of element. Including value type INT. (Translator Note: This seems to be limited, in front of C with static template, it is indeed attractive. In addition to saving space, I hope it can do more. Static template is in use After some small skills, you can also reach the space overhead of the dynamic template, but also have a static type check) 8.2.5 pointer, handle and null

Standard C supports pointer types and empty pointer. C / CLI adds handle and null value, in order to help integrate handles, and there is a universal null value, C / CLI defines the keyword nullptr. This keyword represents an air type. Nullptr is null value constant. (You cannot create an empty type, only ways to get an null constant is through this keyword.

Definition of empty pointer constant (standard C requires expression of compiled compiled time to calculate the result of 0) The resulting NullPtr is extended. Nets can be implicitly converted into a pointer or handle type, which becomes a null pointer value or a null value, respectively. This allows NULLPTR to be used to relatively size, equal, conditional, and assignment.

Object ^ obj1 = nullptr; // handle OBJ1 has a null value.

String ^ str1 = nullptr; // handle Str1 has a null value

IF (Obj1 == 0); // false (0 boxed, then two handles are not equal)

IF (Obj1 == 0L); // false "" "" "

IF (Obj1 == Nullptr); // True

Char * pc1 = nullptr; // PC1 is a null pointer value

IF (PC1 == 0); // true When 0 as a null pointer value

IF (PC1 == 0L); // true "" "

IF (PC1 == Nullptr); // True, this time NULLPRT is a null pointer value

INT N1 = 0;

N1 = nullptr; // error, no implicit conversion from Nullptr to INT

IF (n1 == 0); // true, perform an integer comparison

IF (n1 == 0L); // "" ""

IF (n1 == nullptr); // error, no implicit conversion from Nullptr to INT

If (nullptr); // error does not have implicit conversion from NullPtr to BOOL

IF (nullptr == 0); // error, no implicit conversion from Nullptr to INT

IF (nullptr == 0L); // "" ""

nullptr = 0; // error, Nullptr is not a left value

NULLPTR 2; // Error, Nullptr cannot participate in arithmetic calculations

Object ^ obj2 = 0; // Obj2 is a handle after 0

Object ^ obj3 = 0L; // Obj3 "" "

String ^ str2 = 0; // error, no implicit conversion from int to String ^

String ^ STR3 = 0L; // "" "" "

Char * pc2 = 0; // PC2 is a null pointer value

Char * pc3 = 0L; // pc3 "" "

Object ^ obj4 = expr? Nullptr: nullptr; // obj4 is null value Object ^ obj5 = expr? 0: nullptr; // error, no compatible type

Char * pc4 = expr? nullptr: nullptr; // PC4 is a null pointer value

Char * pc5 = expr? 0: nullptr; // error, no compatible type

INT N2 = EXPR? NULLPTR: NULLPTR; // Error, no implicit conversion to INT

INT N3 = EXPR? 0: Nullptr; // Error, no compatible type

Sizeof (nullptr); // error, no size

Throw nullptr; // error

Void f (Object ^); // 1

Void f (string ^); // 2

Void f (char *); // 3

Void f (int); // 4

f (nullptr); // error, secondary (1, 2, 3)

f (0); // Calls f (int)

Void G (Object ^, Object ^); // 1

Void G (Object ^, char *); // 2

Void G (Object ^, int); // 3

g (Nullptr, Nullptr); // error, secondary (1, 2)

g (nullptr, 0); // Calls G (Object ^, int)

g (0, nullptr); // Error, secondary (1, 2)

Void H (Object ^, int);

Void h (char *, object ^);

h (nullptr, nullptr); // call h (char *, object ^);

h (Nullptr, 2); // Call H (Object ^, int);

Template Void K (t t);

K (0); // specialization K, T = int

K (nullptr); // error, NULLPTR cannot be instantified

K ((object ^) nullptr); // specializes K, T = Object ^

K (nullptr); // specializes K, T = int *

Because the object assigned in the local heap cannot be moved, the object pointer does not need to track the location of the object. However, the object of the CLI pile can move, so they need to be tracked. Similarly, the native pointer is not enough to cope with them. In order to track the object, C / CLI defines the handle (using symbol ^) and track reference (using symbol%)

N * hn = new n; // allocated in native pile

N & rn = * hn; // Bind the original pointer to the normal reference

R ^ hr = gcnew r; // Allocated on the CLI pile

R% rr = * hr; // Binding tracking reference to GC left value (GC-LVALUE)

Usually,% is like ^ like *.

Standard C has a one-membered & operator, C / CLI provides a yuan% operator. When & t produces T * or an Interior_Ptr ,% T generates a T ^.

The right value and the left value continue to have the same meaning of standard C . Follow the following provisions:

• An entity declared type T *, is a native pointer T, pointing to a left finger.

• Apply a dollar * to an entity declaration type T *, which is the decix T *, generate a left value.

• An entity declaration type T &, is a native reference T, is a left value. • Expression & Left value generates T *.

• Expression% left value generates T ^.

A GC Left Value is an expression that references a reference object from a CLI stack or a value included in such an object. There is the following rules for GC-LVALUES:

• There is also a standard conversion from "CV-Qualified Lvalue of Type T to" CV-Qualified GC-LVALUE OF TYPET ", and also exists in" CV-Qualified GC-LVALUE OF TYPE T "to" CV-Qualified Rvalue of Type T) "

(Translator Note: Sorry, I really don't understand what it is saying.)

• A declared type is a T ^ entity, the handle T, pointing to the GC-left value (GC-LVALUES).

• Apply a dollar * for a statement of a declared type T ^, a reference T ^, producing a GC-left value (GC-LVALUES).

• A declared type T & entity, tracking reference t, is a GC-left value (GC-LVALUES).

• Expression & GC Left Value (GC-LVALUE) generates an Interior_Ptr .

• Expression% (GC-LVALUE) generates T ^.

The garbage collector is allowed to move the objects on it on the CLI pile. In order to give a pointer correctly to such an object, the runtime environment needs to update the new location of the pointer to the object. An internal (internal, interior) pointer (defined with interior_ptr) is the pointer updated in this way.

转载请注明原文地址:https://www.9cbs.com/read-65433.html

New Post(0)