Standard conversions for C + + language definitions

Source: Internet
Author: User
Tags access properties arithmetic types of functions volatile

Standard conversions

The C + + language defines conversions between its underlying types. It also defines pointers, references, and conversions to pointer-derived types of members. These conversions are called "standard conversions."

1. Integral type lifting

An object of an integer type can be converted to another wider integer type (that is, a type that can represent a larger set of values). The conversion of this type of extension is called an integer promotion. With integer elevation, you can use the following items at any location where other integer types can be used:

    • charand short int types of objects, literals, and constants

    • Enum type

    • intBit field

    • Enumerator

C + + promotion is a "value hold". That is, the promoted value must be the same as the value before the promotion. In a value retention promotion, an char object of a shorter integer type (such as int a bit field or object of a type) is promoted to the type if it can represent the full extent of the original type int . If the int value of the full range cannot be represented, the object is promoted to the unsigned int type. Although this policy is the same as that used in ANSI C, the value preserving transformation does not preserve the object's "symbol".

Values that retain elevation and retention symbols generally produce the same results. However, if the promoted object is one of the following, they may produce different results:

    • /,,, % /= %= <, <=, > or >= operands

      These operators depend on the symbol used to determine the result. Therefore, when the value retention and symbol retention promotion are applied to these operands, they produce different results.

    • Left operand of >> or >>=

      When performing shift operations, these operators discriminate between the number of signed and unsigned quantities. For a signed quantity, moving the quantity right causes the symbol bit to propagate to the vacated bit position. For unsigned quantities, the vacated bit positions are populated by 0.

    • The arguments of an overloaded function, or the operands of an overloaded operator (depending on the symbol of the type of the operand used for parameter matching).

2. Integral type conversion

Integer conversions are performed between integral types. Integral types include char , int and long (and these types of short, signed, and unsigned versions)

(1) signed conversion to unsigned

An object of the signed integer type can be converted to the corresponding unsigned type. The actual bit pattern does not change when these transformations occur, but the interpretation of the data changes .

 //  conve__pluslang_converting_signed_to_ Unsigned.cpp  //  compile with:/EHsc  #include <iostream> using  namespace    STD;  int   main () { short  i =-3  ;        unsigned  short   U; cout  << (u = i) <<  " \n   Span style= "COLOR: #800000" > " ;  //  output:65533  

In the previous example, it signed short i was defined and initialized to a negative number. The expression (u = i) causes i the conversion to unsigned short before assigning a value u .

(2) unsigned conversion to signed

An object of an unsigned integer type can be converted to the corresponding signed type. However, if the value of an unsigned object is outside the range represented by a signed type, such conversions can cause data to be interpreted incorrectly, as shown in the following example:

 //  conve__pluslang_converting_unsigned_to _signed.cpp  //  compile with:/EHSC  # Include <iostream> using  namespace   Span style= "COLOR: #000000" > STD;  int   main () { short   I; unsigned  short  u = 65533  ; cout  << (i = u) <<  " \n   Span style= "COLOR: #800000" > " ;  // output:-3  

In the preceding example, it u is a short unsigned integer object that must be converted to a signed number to evaluate the expression (i = u) . The data is interpreted incorrectly because its value cannot be signed short correctly represented in.

3. floating-point conversions

    objects of the floating type can be safely converted to more precise floating-point types, that is, conversions do not cause cardinality loss. For example, a conversion from float to double or double to long double is safe, and the value remains the same .

If the object of a floating-point type is in a range that the low-precision type can represent, it can also be converted to that type. (for ranges of floating-point types, see floating-point limits.) If the original value cannot be represented accurately, it can be converted to the next higher or lower value that can be represented. If such a value does not exist, the result is indeterminate.

4. conversion between integral type and floating point (with truncation error)

Some expressions may cause the floating-point object to be converted to an integer and vice-versa. When an integer object is converted to a floating-point type and the original value is not correctly represented, the result is either the next larger, or the next, small, representing value.

  When a floating-point object is converted to an integral type, the decimal part is truncated. No rounding is done during the conversion process. Truncation means that the number 1.3 converted to 1,–1.3 will be converted to –1.

5. Arithmetic Conversion

Many of the two-dollar operators (discussed in expressions with a two-tuple operator) cause the operands to transform and produce the results in the same way. The way these operators cause conversions is called "Common arithmetic conversions." The arithmetic conversions of the operands of different native types are performed as shown in the following table. The behavior of a Typedef type is based on its underlying native type.

One of the operands is a long double type. The other operand is converted to a long double type.
The above conditions are not met, and one of the operands is of type double. The other operand is converted to a double type.
The above conditions are not met, and one of the operands is of type float. The other operand is converted to the float type.
The above conditions are not met (none of the operands are of a floating type). An integer elevation is performed on the operand as follows:

-If one of the operands is of unsigned type long, the other operand is converted to a unsigned long type.
-If the above conditions are not met, and one of the operands is of type long and the other operand is a unsigned int type, then the two operands are converted to the unsigned long type.
-If both of the above conditions are not met and one of the operands is of type long, the other operand is converted to a long type.
-If the above three conditions are not met, and one of the operands is of unsigned int type, the other operand is converted to a unsigned int type.
-If none of the above conditions are met, then the two operands will be converted to a int type.

6. pointer Conversion

In assignment, initialization, comparison, and other expressions, you can convert pointers.

(1) Pointer to class

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

The first case is that the specified base class is accessible and the conversion is unambiguous.

Whether the base class is accessible depends on the type of inheritance used in the derivation. Consider the inheritance explained in.

The following table shows the base class accessibility for the scenario illustrated by the diagram.

From the table:

    • directly from the derived class pointer to the parent class pointer, only sometimes public inheritance, in order to legitimately convert to the parent class pointer;
    • Within a subclass, a direct (a *) this, that is, regardless of inheritance, can be used legitimately, and genjiu A's member access properties, as well as inherited ways to access specific functions.
    • Inside the child subclass, because B private inherits a, the access in sub-sub-c is limited, so the condition is not valid.
Types of Functions derived from

is the conversion of b* to a * legal?
External (non-class scope) functions Private No
Protected No
Public Is
b member function (within the B range) Private Is
Protected Is
Public Is
C member functions (within the C range) Private No
Protected Is
Public Is

in the second case, when you use an explicit type conversion, a pointer to a class can be converted to a pointer to the base class. Only the properties of the base class can be accessed.

The result of such a conversion is a pointer to the part of the object that is completely described by the base class (the "child object") .

The following code defines two classes (that is A B , and), which B derive from A . (For more information about inheritance, see derived classes.) Then define bObject , type B the object and two pointers (and) to the object pA pB .

//Conve__pluslang_pointers_to_classes.cpp//C2039 expectedclassA { Public:      intacomponent; intAmemberfunc ();    }; classD | PublicA { Public:      intbcomponent; intBmemberfunc ();  }; intMain () {B bobject; A*PA = &Bobject; B*PB = &Bobject; PA->amemberfunc ();//OK in class APb->amemberfunc ();//ok:inherited from class APa->bmemberfunc ();//Error:not in class A}

The pointer is pA of type A * , which can be interpreted as "a pointer to an A object of type." bObject ( members (such as BComponent and BMemberFunc ) are unique to the type and B cannot be pA accessed through. pApointers only allow access A to those attributes (member functions and data) of the objects defined in the class.

7. Pointers to functions

If the type void * is sufficient to hold a pointer to a function, the pointer can be converted to a void * type. pointers to functions can be converted into void*.

8 Pointer to void

voidpointers to types can be converted to pointers of any other type, but only for explicit type conversions (unlike in C).

Pointers to any type can be implicitly converted to pointers to types void . Pointers to incomplete objects of type can be converted to void pointers (implicit) and back (explicit). The result of such a conversion is equal to the value of the original pointer. The object is considered incomplete (if the object is declared), but does not provide enough information available to determine its size or base class.

A pointer to any object that is not const or is volatile implicitly convertible to a pointer of type void *.

9. Fixed and variable pointers

C + + will not apply standard conversions from const or volatile type to non-const or volatile type. However, any type of conversion can be specified with an explicit type cast (including unsafe conversions).

  C + + pointers to members (except pointers to static members) differ from regular pointers in that they have different standard conversions. A pointer to a static member is a normal pointer and has the same conversion as a normal pointer.

Ten NULL pointer conversions

An integer constant expression that evaluates to zero

Or to a pointer type of such an expression cast, will be converted to a pointer called a "null pointer". The result of this pointer comparison with pointers to any valid object or function is certainly not equal ( except for pointers to base objects, which can have the same offset and still point to different objects ).

In C++11, the nullptr type should take precedence over the C-style null pointer

11 pointer-expression conversions

 all expressions with an array type can be converted to a pointer of the same type . The result of the conversion is a pointer to the first array element . The following example shows such a transformation:

Char // Array of type char.   Char // Equals &szpath[0].  

Reference Conversions

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

    • The specified base class is accessible (as defined in a pointer to a class).

    • The conversion is clear. (For more information about ambiguous base class references, see multiple base classes.) )

The result of the conversion is a pointer to a child object that represents the base class.

Pointer to member

A pointer to a class member that can be converted in assignment, initialization, comparison, and other statements.

  

pointers to base class members

A pointer to a member of a base class can be converted to a pointer to a member of a class derived from the base class when the following conditions are true:

    • A reverse conversion from a pointer to a derived class to a base class pointer can be accessed.

    • Derived classes are not inherited from a base class in a virtual fashion. "Because virtual methods inherit, the function belongs to the subclass, not the parent"

When the left operand is a pointer to a member, the right operand must be a pointer-to-member type or a constant expression that evaluates to 0. This assignment is valid only in the following cases:

    • The right operand is a pointer to a member of the same class as the left operand.

    • The left operand is a pointer to a member of a class that derives from the right operand in a public but ambiguous way.

. Integer constant Conversions

An integer constant expression that evaluates to zero is converted to a pointer named "NULL pointer." The result of this pointer comparison with pointers to any valid object or function is certainly not equal (except for pointers to base objects, which can have the same offset and still point to different objects).

The following code shows the definition of a i pointer to a member in a class A . The pointer pai is initialized to 0 and is therefore a null pointer.

// conve__pluslang_integral_constant_expressions.cpp   class A  {  public:   int  i;  };     int 0 ;     int Main ()  {  }  

Endl.

Standard conversions for C + + language definitions

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.