C + + Primer Learning Summary 6th Chapter function

Source: Internet
Author: User
Tags volatile

6th Chapter Function

1. Local variables in the outermost scope of the function cannot use the same name as the function parameters because they belong to the same scope scope.

2. The life cycle of a local static variable: initialized when the execution path of the entire program passes through the object definition statement for the first time, and is not destroyed until the entire program terminates , even if the object's function finishes executing.


3. If the parameters of the overloaded function are only the top-level const difference, then it is wrong:


If there is an underlying const difference it can count as overloading.

4. If the parameter of the function is to use a reference (and does not change the value of the referenced object), then it should be defined as a constant reference . because using normal reference parameters restricts the types of arguments that a function can accept.


In the example above, if you pass a constant int to the function get (), then an error occurs.

There is also a more subtle error , if the A function correctly defines a parameter as a constant reference, but the B function still defines a formal reference, then using the B function in a function will make an error:

5. How do I pass an array parameter to a function?

arrays are also objects , only the array name is automatically converted to the first element pointer . arrays also have addresses. . arrays also have pointers. . array pointers are used * The array itself can be obtained by dereference .

One-dimensional arrays:

Multidimensional Arrays: C + + actually has no multidimensional arrays, so-called multidimensional arrays are just arrays of arrays, so the size of the 2nd dimension ( and all subsequent dimensions ) of a multidimensional array is part of the array type and cannot be omitted.

question : Why does the value of the A array above be used (*a) [i] instead of directly a[i] output?

A is a pointer to an array of int[10], then *a is an array of int[10]. Then (*a) [i] is the first element of this int[10] array.

or you can understand that a is a pointer to the first element in this a array of a two-dimensional array int a[10][10] ( the element is a int[10] array ), so * A is the first element of a ( the element is a int[10] array ), so (*a) [i] is the specific int value element.

to the print () function is a pointer to the first element (A[0] array) of the A array, so if you want to output the 10 elements of a[1], you need to a+1, then * (a+1), then take each element (* (a+1)) [i].



6. In- depth Discussion : When you first use the array name, the array name is automatically converted to a pointer to the first element of the array. But what if we want to use the pointer to the array (not just the pointer to the first element)?

An array is also an object , and its address can be obtained by using the & address symbol:

7. What does it mean for char **a this parameter?

below "A char* pointer Span style= "color:red" > " a *a char* pointer, as for this char* The pointer points to a char char* char char* The pointer content is fundamentally illegal, it's not something we can control, it takes the rest of the program to give assurances.

Notice the implementation of the above program .

8. How do I pass an array reference parameter to a function?

array reference parameters to correctly specify all dimensions of the array .

9. Do not return a reference or pointer to a local variable ( Although testing a simple program , This often results in the correct result , But this is a serious error ):




call operator and dot operator, the arrow operator has the same precedence and all conform to the left binding law, so if the function returns a pointer, reference or class object, we Can The member of the result object is accessed with the result of the function call .

C++11 support list initialization return value :


12. The function cannot return an array (because the array cannot be copied), the function can only return pointers to the array:

13. How do I directly define a function that returns an array pointer?

The parentheses above cannot be less .

Const_cast Usage:

Usage:const_cast<type_id> (expression)

This operator is used to modify the ( underlying ) const or volatile properties of a type. In addition to const or volatile adornments, the type_id and expression types are the same .

A constant pointer is converted into a very good pointer, and still points to the original object;

The constant reference is converted to a very literal reference and still points to the original object;

By the above S3 know Const_cast<> () strong turn or keep the previous memory address,


Const_cast<> () can cause the underlying const to disappear, but the memory address remains the same . After using const_cast<> (), the contents of the previous memory can be changed by B, but still cannot be changed by a. The underlying const of a reference and pointer is the same effect as a strong turn.

If you use only const_cast<> () strong to top-level const is meaningless behavior, and compilation cannot pass.


It is The value of a to be assigned to B, even if a to the int type, there is no difference.

Error:invalid use of const_cast with type ' int ', which was not a pointer, reference, nor a pointer-to-data-member type.

Above is the compiler error description. Description Const_cast can only be used on the underlying const of pointers or references. (The third item doesn't know what it is?)

15. How do I declare a function that has only local scope?

In a global definition, but declaring a function in a local scope allows a specific function to have only a local scope. The print is not visible in main, because the print definition is behind the main function, and print is declared in the Get function. If print is defined in front of main, print is also available in main, even if print is not declared.

16. Function default argument, if a function parameter has a default value, then each parameter after it must have a default value. Here are the ways that two functions define default values:

Way One : recommend this way .

Way Two


and if you call a function that has a default value, the an argument always matches the leftmost parameter in precedence  . A local variable cannot be the default argument .

17. Only global variables can be used as default arguments. And the name of the default argument is resolved within the scope of the function declaration, and the evaluation process for those names occurs when the function is called .


. Const and CONSTEXPR modifiers:

We already know that the value of a literal constant is immutable, so it's called a constant. A variable is a piece of storage space in memory that we can read and write to. But sometimes we also need a mechanism that prevents random changes to the value of a variable, and we'll use the Const qualifier (const-qualifier). When defining or declaring a variable, the const qualifier can be placed before or after the type specifier that we have previously spoken, indicating that the value of the variable cannot be changed. It is important to note that if a variable is defined or declared with a const qualifier, it must be initialized. Because the const variable cannot be assigned later. For basic data types, this is also a subtle difference between initialization and assignment: You can initialize a const variable, but you cannot assign a value. For example

The const qualifier is also syntactically a type specifier, but he cannot be used alone and must be used in conjunction with other type specifiers to indicate the nature of immutable (constant). Because the const qualifier is also a type specifier, const and non- const variables are not the same type in C + + , although they are still inextricably linked, we will tell in detail later. All you have to remember now is that int and const int are not the same type.

C + + also provides a more ' strict ' constexpr specifier (constexpr-specifier), when a variable is declared or defined, If the constexpr specifier is used before or after the type specifier, it indicates that the variable is a const variable and that the variable must be initialized. You might say that this const is not the same? constexpr is more rigorous, the expression used to initialize the variable must be a ' constant expression ' . This means that the constexpr variable is a const variable initialized by a constant expression . As for the constant expression, we'll talk about it later, for the basic data type, the constant expression is a literal constant, a variable that constexpr the limit, and a const variable that is initialized by a constant expression. In other words, the value of a constant expression can be determined during compilation.


The value of the constexpr variable must be determined by the compiler at compile time . In the above example, because the value of Nonconst_var is syntactically, the runtime may be changed, so the compilation period cannot be determined and is not a constant expression. because const_var2 is initialized by a very few expressions, const_var2 is not a constant expression. But the declaration, definition and initialization of CONST_VAR2 itself is legal. CONSTEXPR are stricter than const and are not constant expressions for initializing constexpr_var2 and CONSTEXPR_VAR3, so their definitions are wrong.

19. Function pointers and their usage:

When the function name is used as a value , the function is automatically converted to a pointer .

Declare a function with a function pointer parameter:

Use typedef to simplify the definition of function pointers:

C + + Primer 5th edition Chinese version p222-223 page has more content to expand.


C + + Primer Learning Summary 6th Chapter function

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.