C + + Primer Plus reading notes--8th chapter function Exploration

Source: Internet
Author: User
Tags function definition function prototype

The 8th Chapter function exploration and seclusion

1. For an inline function, the compiler replaces the function call with the corresponding function code, and the program does not need to jump to a location to execute the code and then return it. As a result, inline functions run slightly faster than regular functions, but at the cost of requiring more memory.

2. To use an inline function, you need to:

Add the keyword inline before the function declaration;

Add the keyword inline before the function definition;

3. When a programmer requests a function as an inline function, the compiler does not necessarily meet this requirement. It may think that the function is too large or notice that the function called itself (inline functions cannot be recursive).

4. Must be initialized when declaring a reference variable

int rat = 101;

int & rodents = rats;

C language can only be passed by value, passing by reference is a new feature of C + +.

    1. 5. If the programmer's intention is to have the function use the information passed to it without modifying the information and to use the reference, then use a constant reference.

Double Refcube (const double &ra);

6. When the function argument is a constant reference, when is the temporary variable created?

There are two types of situations:

The type of the argument is correct, but not an lvalue;

The arguments are of an incorrect type, but can be converted to the correct type.

Double Refcube (const double &RA)

{

Return RA * ra * RA;

}

Long edge = 5L;

Double c5 = refcube (Edge);

Double C6 = refcube (7.0);

In both cases, the compiler will generate a temporary anonymous variable and have the RA point to it. These temporary variables exist only during function calls, and the compiler is free to delete them.

7. For C + + functions referenced by a cosnt parameter, if the argument type does not match, its behavior is similar to passing by value, to ensure that the original data is not modified, a temporary variable is used, the value of the parameter called by the function is passed to the anonymous temporary variable, and the argument is referred to as the variable.

8. If the function parameter is not a constant reference, but a normal reference, the argument cannot be an lvalue, and must be of the exact type, or the compilation will fail.

Double Refcube (double &ra)

{

Return RA * ra * RA;

}

Double x = 3.0;

cout << Refcube (x + 2.0);//error, at which time the argument cannot be an lvalue

Long A = 3;

cout << Refcube (a);//error, at which time the argument type and parameter do not match

If the parameter is changed to const double &RA it will compile successfully. Why is it? P263

If the intent of the double Refcube (double &ra) function is to modify the variable RA, creating a temporary variable will block the implementation of the intent, and the workaround is to prohibit the creation of temporary variables, as is now the C + + standard. and double Refcube (const double &RA)

The purpose of the function is to use only the values passed, not to modify them, so the temporary variable has no adverse effect, and instead makes the function more generic in terms of the types of parameters that can be processed.

9. There are three reasons to declare a reference parameter as a reference to constant data:

Use const to avoid programming errors that unintentionally modify data;

Using const enables a function to handle const and non-const arguments, otherwise it can only accept non-const data;

Using a const reference enables a function to correctly generate and use temporary variables so that the function can handle more parameter types.

10. The purpose of introducing references is for structs and classes, not for basic data types.

One. P268 Why const is used to reference return types

If the return type of the function is a type reference, you can use it as an lvalue. This is because the left-hand expression of the assignment statement must return a modifiable block of memory, and the function return reference does identify a block of memory.

Accumulate (dup, five) = four;

The general return type is the right value. Because such a return value is in a temporary memory unit and runs to the next statement, they may no longer exist.

Suppose you want to use a reference return value, but do not allow operations like accumulate () to be assigned, simply declare the return type as a const reference.

12. Another feature of inheritance is that a base class reference can point to a derived class object without forcing a type cast.

Ostream is a base class and Ofstream is a derived class

14. For a function with a parameter list, you must add a default value from right to left. That is, if you also set a default value for a parameter, you must provide a default value for all parameters to the right of it.

int Harpo (int n, int m = 4, Int j = 5);

The argument must be assigned to the corresponding parameter from left to right, without skipping any parameters.

Beeps = Harpo (3, 8); is not allowed.

15. Function overloading is also known as function polymorphism. The key to function overloading is the parameter list of the function (that is, the function feature label). When the compiler examines a function feature, it treats the type reference and the type itself as the same feature label.

How does C + + keep track of each overloaded function? It places each function name in place (name decoration) According to the parameter type specified in the function prototype.

17. Before c++98 add Keyword typename, C + + uses the keyword class to create a template.

p285~p288 display materialized.

For a given function name, you can have non-template functions, template functions, explicit materialization of target functions, and their overloaded versions.

The prototype and definition of an explicit materialization should begin with template<> and indicate the type by rank.

Materialization takes precedence over regular templates, rather than template functions over materialization and general templates.

19. To learn more about templates, you must understand the terminology instantiation and materialization P288.

20. Including the function template in the code itself does not generate a function definition, he is just a scenario for generating a function definition.

Template <typename t>

void Swap (t &a, T &b)

{

T temp;

temp = A;

A = b;

b = temp;

}

int i = ten, j = 20;

Implicitly instancing Swap (i, J)

Displays the instantiated template void swap<int> (int &, int &); The direct command compiler creates a specific instance, and a swap template is used to generate an instance of type int.

Explicit materialization uses one of the following two equivalent declarations:

Template <> void swap<int> (int &, int &);

Template <> void swap (int &, int &);

The explicit materialization means "Do not use Swap () templates to generate function definitions, but function definitions that are explicitly defined for type int". These two prototypes must have their own function definitions.

An explicit materialization declaration contains <> after a template, while an explicit instantiation does not.

22. Implicit instantiation, explicit instantiation, and explicit materialization are collectively referred to as materialization.

23. Development of 8.5.6 template function p295-p297

C + + 11 keyword Decltype

Post return type (trailing return)

C + + Primer Plus reading notes--8th chapter function Exploration

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.