In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to apply the citation of C++". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought slowly and deeply. Let's study and learn how to apply the citation of C++.
In C++, a reference is an alias for a variable that needs to initialize itself with another variable or object. Quotation is like a person's nickname, for example: there is a person whose name is Zhang Xu, because he is the third at home, and others give him a nickname Zhang San, so that we are called Zhang San or Zhang Xu, referring to the same person. The following code declares a reference b and initializes it with the variable a.
Int a = 5; int & b = a; / / declare a reference with &. References must be initialized at declaration time
Consider the following code:
Int a = 5; int & b = a; int cations 3; / / instead of turning b into a reference to c, it assigns a value to b, where the values of b and a both become 3
The reference and the variable used to initialize the reference point to the same block of memory, so the contents of the same block of memory can be changed by reference or variable. Once a reference is initialized, it represents a specific piece of memory and can no longer represent other memory.
So what's the difference between references and pointer variables?
A reference is just an alias, an alternative name for a variable or object. The referenced address does not make any sense, so C++ does not provide a way to access the address of the reference itself. The address of a reference is the address of the variable or object it references, and what you do to the address of the reference is what you do to the address of the variable or object that is referenced. The pointer is the address, and the pointer variable stores the address value, so to take up storage space, we can modify the address value saved by the pointer variable at any time to point to other memory.
The memory model of reference and pointer variables is shown in the following figure:
When writing a program, it is rare to use references directly, that is, to initialize a reference with a variable. If you do so, you can modify the contents of the same block of memory through both variables and references. In the program, it is easy to have problems and do not know what the value in memory is at this time.
References are mostly used in the definition of formal parameters of a function. When we call a function to pass parameters, we often use pointers to pass, one is to avoid copying the values when the arguments occupy a large amount of memory, and the other is to complete some special functions, for example, to modify the content of the argument pointing to memory in the function. Similarly, using a reference as a formal parameter of a function can also complete the function of a pointer, and in some cases it can achieve better results than using a pointer.
Next, we will quote the end of this section with an explanation of a program.
Example:
# the include / / change function is mainly used to exchange the values of an and b, void change (int& a _ line intt & b); void main () {int xanth5; int yellow3; cout
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.