Chapter 9 Spectral Conversion

xiaoxiao2021-03-06  14

Chapter 9 Spectral Conversion

The C language defines the conversion between its basic types, and also defines the conversion of pointers, reference, and members pointers. These conversions are called "standard conversion" (more information about types, standard types, and derived types, "Types" in Chapter 2, "Basic Concept".)

This chapter discusses the following standard conversion:

* Integer improvement

* Integer conversion

* Floating point conversion

* Floating point and integer conversion

* Arithmetic conversion

* Pointer conversion

* Reference conversion

* Member pointer conversion

Note: The user-defined type can specify their own conversion, and the user-defined type of conversion is included in the "Constructor" and "Conversion" of Chapter 11 "Special Member Functions".

The following code generates a conversion (in this example is an integer):

Long lnum1, lnum2;

Int inum;

// Inum increases before assignment to type long

lnum1 = inum;

// Inum increases to type before multiplication

lnum2 = inum * lnum2;

Note: The result of the conversion is a l value only when the conversion generates a reference type. For example, a user-defined conversion description is:

Operator Int & ()

Returns a reference, it is a l value. However, the conversion description is: Operator int () returns an object and is not an L value.

Interest

A integer object can be converted to another wide integer (ie, one can represent a larger numerical set type). This broaden wide type of conversion is called "integer improvement". With an integer increase, you can use the following location in another integer type:

* CHAR and SHORT INT type objects, text and constants

* Enumeration Type

* INT bit

* Enumerator

C improvement is "value reserved", that is, the value after the increase is ensured as the value before the increase. In the lifting of values, if int shows all ranges of source types, shorter integer objects (such as bit fields or char type objects) are enhanced to int types. If int cannot represent all ranges of values, the object is upgraded to the unsigned int type. Although this strategy is the same as the use of ANSI C, the value retains the conversion and does not retain the "symbolicity" of the object.

Value retention increases and reserved symbolic lifting usually produces the same result, but if the lifted object is one of the following, they produce different results:

One of the operands of /,%, / =,% =, <, <=,> or> =.

These operators determine that the results are dependent on the symbol, so value retention and symbol retention increase produce different results when using these operands.

>> or >> = left operation

This operator is different when performing a shift operation, and the unsigned amount is different. For the signs, the right shift is caused to enter the vacancy; for unsigned quantities, the space is dropped.

* One parameter of an overload function or a heavy-duty operator that relies on the type of the type of parameter matching operand (more information on the operator defined to define the overload, see "Heavy Duty" in Chapter 12 Carrying operator ").

Integer conversion

Integer conversion between integer types, integer types are CHAR, INT, and LONG (and these types of short, signed, and unsigned versions).

This section describes the following types of integer conversion:

* There is a symbolic turn to no symbolic

* No symbolic turn is a symbolic

* Standard conversion

Sign with symbolic

The symbol-type integer object can be converted to the corresponding non-symbol type. When these conversion occurs, the actual bit mode has not changed, but the number explanation changes, consider the following code:

#include void main () {Short i = -3; unsigned short u; cout << (u = i) << "/ n";

Its output is as follows:

65533

In the above example, a Signed Short variable I is defined, and is initialized to a negative number, the expression (u = i) converts I to the Unsigned Short type prior to assigning u.

No symbolic turn to have a symbolic

The unsigned integer object can be converted to the corresponding symbol type. However, if the range of the value of the symbolic object is exceeded that the symbol type can represent the scope, this conversion will result in the malfunction of the data, as shown in the following example:

#include

Viod main ()

{

Short I;

UNSIGNED SHORT U = 65533;

COUT << (i = u) << "/ n"}

Its output is as follows: -

3 In the above example, u is a unsigned short integer object, which must be converted to a symbol amount to match the evaluation of the expression i = u, because its value is not normally represented in the Signed Short, so data is misused For the above aspects.

Standard conversion

The integer object can be converted to a shorter symbol or unsigned integer type, which is called "standard conversion".

If the value of the source object exceeds the range represented by a shorter type, the conversion will cause data loss. Note: When the transition to the shorter type occurs, the compiler gives an advanced warning.

Floating point conversion

A floating-point type object can be securely converted to a more accurate floating point type, that is, the conversion does not cause loss effective bit. For example, the conversion from float to double or from Double to Long Double is secure, and the value has not changed.

A floating-point type object can also be converted to a low-precision type as long as it can represent the type of "floating point limit" in Chapter 2, "Basic Concept" in Chapter 2, "Concept of Floating Point Types"). If the source value cannot be explicitly represented, it can be converted to a neighboring higher or lower representative value. If this value does not exist, the result is uncertain, considering the following example

: cout << (float) 1E300 << ENDL;

Float types can represent the maximum value of 3.402823466E38, a number smaller than 1e300. Therefore, the number is converted to infinity, and the result is 1. # INF.

Floating point and integer conversion

Some expressions can cause floating point type objects to convert to integer, or vice versa.

This section describes the floating point and integer conversion of the following types:

* Floating point to integer

* Interest to floating point

Floating point to integer

When an object of a floating point type is converted to an integer type, the fractional portion is cut off, and the rounding is not rounded during the conversion process, and the cut-off means that the number of such as 1.3 is converted to 1, and -1.3 is converted to -1 .

Integer to floating point

As a full object is converted to a floating point type and the source value cannot be accurately represented, the result is a higher or lower representative value adjacent.

Arithmetic conversion

Many binary operators (discussed in the "expression of binary operators in Chapter 4" Expressions "in Chapter 4) lead to the conversion of the operand and produce results in the same manner, which cause the conversion to be called" Common arithmetic conversion ".

The implementation of the arithmetic conversion of different types of operands is shown in Table 3.1.

Table 3.1 Conditions for Type Conversion

Satisfying Condition Conversion Two Operations One One For Type Longdouble Another Operator Converted to Type LongDouble The condition is not satisfied, one of the operands is that the other operand is converted to the type Double Double Double condition is not satisfied, where One operand is that the other operand of floating-point is converted to the integer improvement of the type float to perform the integer improvement: the front condition is not satisfied (no operands are float types) * If one of the operands is type unsignedlong The other operand is converted to the type unsigned long * If the previous condition is not satisfied, and if one of the operands is the type LONG and the other is the type unsigned int, the two operands are converted to the type unsigned long. * If the previous two conditions are not satisfied, and if one of the operands is type LONG, the other operand is converted to the type of LONG. * If the three conditions in front are not satisfied, and if one of the operands is the type unsigned int, the other operand is converted to the type unsigned Int. * If there is no satisfaction, the two operands are converted to type int.

The following code illustrates the conversion rules described in Table 3.1:

Float fval;

Double DVAL;

INT IVAL;

Unsigned long ulval;

Dval = ival * ulval; // ival Convert to unsigned long type; multiplication results are converted to Double type.

Dval = ulval fval; // ulval Convert to FLOAT type; the result of the addition is converted to a Double type.

The first statement in the above example gives two integer type IVAL and ULVAL multiplication. The condition satisfaction is that there is no operand is a floating point type and one of the operands are unsigned int, so another operand IVAL is converted to the type unsigned int. The result is assigned to DVAL. The condition is that the operand type is Double, so the multiplication Unsigned int result is converted to type Double. The second statement in the above example gives a FLOAT and an integer type ie fval and ulval addition. ULVAL variables are converted into type float (third condition in Table 3.1), the result of the addition is converted to type Double Double (The second condition in Table 3.1), and assigned to DVAL.

Pointer conversion

In assignment, initialization, comparison, and other expressions can be converted, this section describes the following pointer conversion:

* Empty pointer

* Void type pointer

* Object pointer

* Function pointer

* Class pointer

* Expression

* Modified pointer with const or volatile

Empty pointer

One integer constant expression or shape of 0 is converted into a pointer, referred to as a "empty pointer". This pointer ensures that it is not equal to the pointer of any effective object or function (except for a pointer to the basic object, this pointer can have the same offset but point to different objects).

Void type pointer

The Void type pointer can be converted to any other type of pointer, but you must use an explicit type shape (unlike C language) (see Chapter 4 "Expression" with explicit types in Chapter 4 for more information on type styles. Expression of the conversion "). Any type of pointer can be implicitly converted to a VoID type pointer.

A pointer to a type of incomplete object can be converted to a VOID pointer (implicitly) and can be reversed (explicitly), which is equal to the value of the source pointer. An object is illustrated, but there is not enough information to determine its size or base class, it is considered incomplete.

Object pointer

Any pointer to the object of the Const or Volatile can be implicitly converted to a VOID * type pointer.

Function pointer

A function pointer can be converted to a VOID *, if the type VOID * is large enough to keep the pointer.

Class pointer

In both cases, a class pointer can be converted to a base class pointer.

The first case is when the specified base class is accessible, and the conversion is not blurred (more information about the fuzzy base class reference "multiple base classes") in Chapter 9

Whether a base class can access depends on the type of inheritance in the derived, considering the inheritance described in Figure 3.1:

Table 3.2 gives the base type accessibility of the situation described in Figure 3.1.

Table 3.2 Base Category Accessibility

The function type derives whether the transformation of B * to A * is legally external (non-class) function privately no public is a b-member function (in the B-range) private is the public is a common C member function ( In the C range) The private no protection is the public.

A class pointer can be converted to a base class pointer is when you use explicit type conversion (more information on explicit type conversion, see Chapter 4 "Expression" in Chapter 4 Expression of type conversion ").

The result of this conversion is a "sub-object" pointer, which is completely described by the base class.

The following code defines two classes A and B, where b is derived (for inheritance, see Chapter 9 "Derivation class") and defines an object BOBJECT of type B, and two pointers pointing to the object (PA and PB).

Class a {public: int Acomponent; int amemberfunc ();}; Class B: public a {public: int bcomponent; int bmemberfunc ();

B bobject;

A * PA = & bobject;

B * PB = & bobject;

PA-> amemberfunc (); // is feasible in class A

Pb-> amemberfunc (); // Viable: from the inheritance from class A

PA-> bmemberfunc (); // Error: Not in class A

The pointer PA is type A *, which can be interpreted as "a pointer to the type A", a BOBJECT member (for example, bcomponent and bmemberfunc) is unique, so it cannot be accessed by PA, and the PA pointer only allows access to the class A features of the object defined (member functions and data).

Pointer expression

Any expression of array types can be converted to a pointer to the same type, and the result of the conversion is a pointer to the first array element. The following example illustrates this conversion: char szpath [_max_path]; // char type array

Char * pszpath = szpath; // is equal to & szpath [0]

An expression result is a function that returns a particular type of function, which can be converted to a pointer to the type of function, except:

* This expression is used as an operand of the address operator (&).

* This expression is used as an operand to function call operators.

Modified pointer with const or volatile

C does not provide a standard conversion to non-Const or Volatile to a non-Const or Volatile, but any type of conversion can be specified using explicit type shape (including unsafe conversion).

Note that the C member pointer is except for static member pointers, and there is no different standard conversion with the normal pointer.

The static member pointer is a common pointer and has the same conversion as a normal pointer (see "Class members" in Chapter 2, "Basic Concept").

Reference conversion

A category reference can be converted into a base class in the following cases:

* The specified base class is accessible (as defined in the "class pointer" in front of this chapter)

* Conversion is non-fuzzy (more information about the fuzzy base category) "Multi-Class Class" in Chapter 9 "Delivery Class").

The result of the conversion is a pointer to the sub-object of the base class.

For more information on references, see "Object reference" in Chapter 2, "Basic Concept".

Member pointer conversion

The class member pointer can be converted during assignment, initialization, comparison, and other expressions. This section describes the following member pointer conversion:

* Integrated constant expression

* Base class member pointer

Integral constant expression

One integer constant expression equal to 0 is converted to a pointer called "empty pointer". This pointer ensures that it is not equal to any effective object or function (except for the basic object pointer, this pointer can have the same offset but point to different objects).

The following code gives a definition of a pointer to the member i of class A. The PAI pointer is initialized to 0, namely the empty pointer:

Class a {public: int I:};

INT A :: * PAI = 0;

Base class member pointer

When the following conditions are satisfied, the base class member pointer can be converted to a member pointer from the class derived class:

* Inverse transformation, from the derived class pointer to the base class pointer, is accessible.

* Derived class does not inherit from the base class.

When the left operating number is a member pointer, the right operand must be a member pointer type or a constant expression equal to 0.

This assignment is only effective in the following situations:

* The right operand is a member pointer that is the same as the left operation.

* Left operation is a pointer to a class member that is non-blurred from the right operation.

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

New Post(0)