C++primer Study note "5"

Source: Internet
Author: User
Tags case statement

Type conversions exist in a lot of C + + code, and more are implicit type conversions that are performed behind the scenes you don't see. The conversion between arithmetic types is to prevent the loss of precision. If the left and right operands are inconsistent and have the conditions for the conversion, the left-hand operand is converted to the leftmost operand type.

Where type conversions occur: Types that represent a small range in a mixed expression are converted to a type that represents a larger range, and when the arithmetic type is used as a conditional expression, the arithmetic type is converted to the bool type.

Cosmetic enhancement: The integral type is smaller than int (char,signedchar,unsigned char,short,unsigned) if the range of these types can be contained within the int type, then it is converted to an int type, if it is outside the range of the int representation, Then convert to the unsigned int type. If the bool is promoted to int, then false is converted to 0,true to 1.

Signed and unsigned conversions: This type of conversion is dependent on the relative size of the integral type in the machine, because the transformations designed to unsigned in the expression are machine-dependent. When unsigned short is contained in int, it is converted to int, otherwise two is converted to unsigned int. If unsigned int is contained in long, that unsigned int is converted to long; otherwise two is converted to unsigned long.

The conversion between signed and unsigned int is often unexpected. Signed in an expression are converted to unsigned int. If the signed happens to be negative, it will be converted into a very large value, which can cause side effects. As for why a negative number converted to unsigned will become a very large value, which involves the computer on the symbolic integer type of storage mechanism and operation mechanism.

In most cases, the array name is converted to a pointer of the same type that points to the first element in the array.

Arithmetic type 0 is converted to false in bool, and other values are converted to true. However, converting from bool to Integer, false to 0,true specifies a conversion of 1.

In C + +, the enumeration type is automatically converted to an integer, as for the conversion of the integer type, which relies on the maximum value of the machine and the enumeration member, the principle is the smallest type that contains the maximum value. If the member value of the enumeration is within the range of int, it is converted to int, otherwise it is converted to unsigned int,long,unsignedlong, and so on.

When a const object reference is initialized with a non-const object, it is converted to a const object;

In C + +, you can use the type conversion method of C, and also provide 4 keywords that are specifically used to display type conversions. They are static_cast,const_cast,dynamic_cast,reinterpret_cast respectively. The traditional conversion can be done with static_cast; the only use of const_cast is to remove the const and volatile attributes, and the dynamic_cast is used for runtime type recognition, mainly for the object pointers under the class inheritance system The implementation of the reinterpret_cast relies on the machine, which is primarily used to re-interpret the bit patterns of the operands at a lower level, which is based on the representation of the user's very familiar type and the compiler's case.

In the Switch-case statement, each case (label) is a constant expression. In a switch statement, you can define a variable only after the last case statement or the default statement: Prevents the code from skipping the definition and initialization of the variable.

An ASSERT (assertion mechanism) is a pre-processing macro that detects the true and false expressions in parentheses only if no debug macros are defined. Once the development and testing work is done, the program is established and the debug macro is defined. In the product code, the assert will not work.

The standard library function terminate function is invoked when an exception is generated in the program, and the program is terminated.

Using the Invoke operator in C + + (which is actually a pair of parentheses, but also an operator, and can be overloaded, the function object is based on this) implements the call to the function.

In earlier versions of C + +, functions could have no return type, which by default is int. But now the C + + standard stipulates that the return type must be returned, but the main function is an exception. The standard of the main function in C + + is that the return type is int, and when executed to the last found no return value, 0 is returned automatically, indicating the normal end.

Each function call will open up the local space of the parameter and initialize it with an argument.

int and const int are not overloaded in the formal parameter list of a function, but const can be used as an overloaded identifier after a function.

A parameter of a non-const reference can only be associated with a reference of exactly the same type:

INTINCR (int &val)

{Return ++val; }

Intmain ()

{

Short V1 = 0;

Const int v2 = 1;

INT v3 = incr (v1); Error because V1 is not an integral type and does not accept convertible types

V3 = INCR (v2); Error because v2 is a const object

V3 = incr (0); Error because 0 is not a left value

V3 = incr (v1 + v2); Error because V1 + v2 is an expression, not an lvalue

Int v4 = incr (v3); Right, exactly the same type

}

Void printvalues (int *);

Void printvalues (int []);

Void printvalues (int [10]); These three function declarations are equivalent because the compiler ignores the array length of the formal parameter, and only detects if the argument conforms to the same type pointer, and does not detect the array length information for the argument.

To pass an array by reference:

As with other types, arrays have references, but they are not allowed to hold references within array elements, so only array references, no reference arrays.

If the parameter is a reference to an array, the compiler does not convert it to a pointer, but instead passes the array itself, in which case the compiler checks if the array length of the argument is consistent with the formal parameter, and the inconsistency compiles the error. So passing an array reference guarantees that the code inside the function is within the size range of the array.

Fun (int (&array) [10]); passing array references

Fun (int &array[10]); Formally similar to a reference array, but without this noun, a grammatical error

When passing multidimensional arrays, the compiler ignores the detection of the first dimension length:

Func (int (*array) [10]);

Fun (int array[][10]); The two sentences are equivalent.

Any program that handles an array ensures that the program stays within the bounds of the array. There are three ways to do this: 1 at the end of an array, similar to the null character of a C-style string, and a pointer to the first element of the array and the next position of the last element, respectively, when passing a parameter, similar to the left-hand-open principle in the standard library; 3 The length of the passed array

Functions with deformable parameters: the ellipsis parameter in C + + is for compiling a C language program that uses varargs. For C + + programs, you can only pass simple basic type data to a function that contains an ellipsis parameter. You can use an ellipsis parameter when you cannot accurately list the type and number of all arguments for a function. The ellipsis type check is paused.

Void foo (parm_list, ...); With a specific number of formal parameters

Void foo (...); Without formal parameters

Most functions with an ellipsis parameter take advantage of some of the information in the displayed arguments to get the type and number of other optional arguments. The previous case is more commonly used.

If the function return type is not a reference, the function is called out to copy the return value of the function to the temporary object.

If the function return value is a reference, no replication is generated. But it's important to be clear that the returned reference is the object that is associated with, and cannot return a reference or pointer to a local object.

C++primer Study note "5"

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.