C ++ Study Notes-inline functions, references, and study notes

Source: Internet
Author: User

C ++ Study Notes-inline functions, references, and study notes

This article is an original work. For more information, see the source.

Welcome to my blog: http://blog.csdn.net/hit2015springand http://www.cnblogs.com/xujianqing/

Author: follow-up in the morning

I have been thinking about writing C ++ Study Notes for two months. I have been learning C ++ for two months. Today I want to write some concepts and functions of references, inline functions, and so on. Let's get started!

Inline functions:

The program we write will eventually use the compiler to compile the link to form a binary code that can be known by machines, and then store it in a memory. At this time, each piece of program code will have its own address, the computer increases by 1 according to the address and executes the code in sequence. When the code calls other functions, it needs to store many states of the current program execution and put these items into the stack, then execute the called function, and then return to the original program breakpoint to continue the execution. In this way, it will waste time and some memory. As a result, the inline function appears, which is used to replace the original function call with the called function code at the position of the called function in the stage when the compiler links the code, in this way, you do not need to call it to consume the time and memory. If you call an inline function in ten places in your program, this function will have ten copies in the entire code. Figure-based speech:

To use an inline function, you must take one of the following two measures:

  • Add the keyword inline before the function declaration
  • Add the keyword inline before Function Definition

The most common usage is to define functions and enable them together,

Code on;

# Include <iostream>

Using namespace std;

Inline double square (double x) {return x * x ;}

 

Int main ()

{

Double a, B;

Double c = 11.0;

A = square (5.0 );

B = square (4.5 + 7.5 );

Cout <"a =" <a <endl;

Cout <"B =" <B <endl;

Cout <"square (C ++) =" <square (c ++) <endl;

Cout <"now c =" <c <endl;

}

Inline Function summary:

Inline functions appear to save some time and memory occupied by function calls. It is obviously not of great significance to use inline functions when some functions are relatively long, the execution time is much higher than the call time. Therefore, if the function definition exceeds multiple rows, inline functions are not used and recursion is not allowed.

Reference

If a person is called "A cat" and its name is "a dog", then you call "a cat" or "a dog" and everyone understands it. So when you hit "A cat, "A dog" will also be hurt (the same person ). The reference is like this. The same variable is called two names. After you modify a variable named "A cat", the variable named "A dog" must have changed. (Remember to change it wherever they are ).

For example, set the int variable firstName to the small name lastName;

Int firstName;

Int & lastName = firstName;

After obtaining the name, the two names are always dependent. The Web tracking is endless. He started his story:

Note: You must initialize the referenced variable when declaring the referenced variable. As shown below:
Int first Name;

Int & lastName;

LastName = firstName; no .... No ..... No ....

What is the use of referenced variables?

In the process of passing a function by value, modifying the form parameter does not change the actual parameter because the passed value is copied first during the calling process and then processed using this copy. In this way, the called program will not access the variable in the called program.

When a call by value encounters a large struct, copy it and use a copy to take up the time. Therefore, a call function is passed by reference variable. This is equivalent to directly performing operations on the original value. OK, you do not need to copy it. Of course, you can also use a pointer to access it. The same effect is that the pointer is not quite clear. If you call it by reference, you only need to call the program by passing the call by value.

If you call a pointer, you can copy the pointer and then perform a series of operations with the copied copy.

A simple example:

# Include <iostream>

Using namespace std;

Void swapr (int & a, int & B); // call according to the reference

Void swapp (int * p, int * q); // call by pointer

Void swapv (int a, int B); // call by passing the value

 

 

Int main ()

{

Int wallet1 = 100;

Int wallet2 = 200;

Cout <"wallet1 =" <wallet1 <endl;

Cout <"wallet2 =" <wallet2 <endl;

Cout <"reference transfer" <endl;

Swapr (wallet1, wallet2 );

Cout <"wallet1 =" <wallet1 <endl;

Cout <"wallet2 =" <wallet2 <endl;

Cout <"pointer passing" <endl;

Swapp (& wallet1, & wallet2 );

Cout <"wallet1 =" <wallet1 <endl;

Cout <"wallet2 =" <wallet2 <endl;

Cout <"pass by value" <endl;

Swapv (wallet1, wallet2 );

Cout <"wallet1 =" <wallet1 <endl;

Cout <"wallet2 =" <wallet2 <endl;

}

 

 

Void swapr (int & a, int & B) // call according to the reference

{

Int temp;

Temp =;

A = B;

B = temp;

}

Void swapp (int * p, int * q) // call by pointer

{

Int temp;

Temp = * p;

* P = * q;

* Q = temp;

}

Void swapv (int a, int B) // call by passing the value

{

Int temp;

Temp =;

A = B;

B = temp;

}

Analyze the program:

When calling a function based on the reference transfer, specify the reference of wallet1 as a and the reference of wallet2 as B, so that two values in the function are exchanged, you can modify the original value. During value-based transfer, the original value is not changed, so the exchange is not successful.

Summary:

Reasons for reference:

  • The programmer can modify the data objects in the call function.
  • By passing references rather than passing the entire data object, you can increase the running speed. This is also a reason for the existence of pointer parameters.

The following are some guiding principles:
Use the passed value instead of modifying the original value:

  • If the data object is small, it is passed by value.
  • The data object is an array, the pointer is used (no choice) and the pointer is declared as a pointer to the const
  • When the data structure is large, use the const pointer or const reference
  • The data object is a class object that is referenced by const.

The function that uses the passed value and modifies the original value:

  • The data object is a built-in data type, using a pointer
  • The data object is an array and the pointer is used.
  • Data Objects are structured and use pointers or references
  • The data object is a class structure that uses references

     

 

 

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.