Next, "Reference"

Source: Internet
Author: User

Netbeans6.1 was installed again in the evening, but it was not used for Java, but for C ++. Alas, no, who told me that it was suspended last semester. I think it's actually cool to think about myself. A C ++ volume is answered by C #.ProgramI don't know what it is, and my rebellious mind suddenly bloomed. I thought it was boring to write programs on paper. I wrote a few "Same as above", "… ... ", The result is ......, However, the teacher was actually pretty good. When asked him later, he said that the last question gave me a full score (only this question was complete), which means that he still didn't care too much about the language.

 

In other words, C ++ is not so hateful, although C # is much better than its elegant. The C ++ review is quite easy to accept, but in some cases, I don't really pay attention to it. I just saw the reference in C ++, so I just pulled out the word and pulled two sentences.

 

In C #, Reference refers to the memory address. For example, if you create a new object person P = new person, is the address of the person instance (allocating memory on the hosting stack), and P itself is a variable on the stack.

 

In C language, there is a pointer concept and a reference concept in C ++. It seems that references in C ++ are basically the same as references in C, but in C ++, there are often many explanations, because pointers exist. What is the difference between pointer variables and references.

 

First, the pointer is not a pointer variable, the pointer is a memory address, and the pointer variable is a variable that stores the address, just like the person P = new person () in C (), this P can actually be called a pointer variable. It is a variable first, and then its stored value is a memory address (of course, it is not called in C ).

 

The reference in C ++ is actually the "alias" of the variable, for example: int A = 10; then, A is a piece of memory space opened up on the stack, storing 10, the reference (alias) stored by a, that is, the integer value 10 of the memory space in which a is located, can be replaced by an alias, I can use the literal meaning of "alias" to better understand it. The alias of A can be considered to be the same except that the name is different from that of.

 

The alias (reference) of A is defined as follows:

Int A =   10 ;
Int   & B = A; // Define reference B of

 

You can have a deeper understanding of parameter passing through functions. See:

 

1 Void Swap ( Int A, Int B );
2 // ...
3 Int C =   10 ;
4 Int D =   12 ;
5 Swap (c, d );

 

When swap (c, d) is called, there is a copy process. In the swap function body, A and B are only copies of the variables C and D. they store the same content, however, the memory space is not the same.

 

Let's look at it again:

1 Void Swap ( Int   * A, Int   * B );
2 //
3 Int C =   10 ;
4 Int D =   12 ;
5 Swap ( & C, & D );
6

 

When swap (& C, & D) is called, there is still a copy process, but it is different from the above. The above copy is a variable, here, we copy pointer variables (memory addresses). That is to say, during the call, it creates two pointer variables and then assigns the addresses of C and D to them respectively, when the swap function is executed, the two pointer variables can be operated. That is to say, we can get the real parameter address from these two pointer variables, of course, there is no problem in changing the value of the real parameter. Note that two pointer variables are created here ".

 

Let's look at it again:

1 Swap ( Int   & A, Int   & B );
2 //
3 Int C =   10 ;
4 Int D =   12 ;
5 Swap (c, d );

 

The alias is used here. The swap function accepts two parameters, which are variables references (aliases). When swap (c, d) is called, the copy process is no longer available, in the swap function body, C and D are directly manipulated because their aliases are passed. This is like Shui yanmu is my network name (alias ), A slap in the face of others is actually a slap in the face of me.

 

Here, let's look at the ref keyword in C:

 

1 Int Swap ( Ref   Int A, Ref   Int B) {... }
2 //
3 Int C =   10 ;
4 Int D =   12 ;
5 Swap ( Ref C, Ref D );
6

 

In fact, the ref function is the same as the reference (alias) in C ++ above. When ref is used, the parameter copy process for parameter passing will no longer exist, if no ref is used, A and B in the SWAp function body are actually copies of C and D.

 

In C #, there is no pointer. Is it similar to passing a pointer in C ++? Yes. See the following:

 

1 Swap (person P) {... } // The semantics of the SWAp function is not considered here to reduce the number of words.
2 //
3 Person =   New Person ();
4 Swap (person );

 

Person is the Instance name (Object Name) of the class. It stores the address of the instance in the managed heap. As mentioned above, we can think of it as a pointer variable in C ++, when swap (person) is called, there is a copy process: opening up a space on the stack to store the person value (the value is the address of the person instance ), then the P operated in the SWAp function body is actually another space on the stack, because it is the address of the Instance like the content stored by the real parameter person, the assignment of the P attribute in the function can change the corresponding property value of the real parameter person. However, if there is a sentence P = new person () in SWAP, then the following P, it points to the space on the other heap (the address of the new person instance ).

 

If you use Ref:

 

1 Swap ( Ref Person P) {...}
2 //
3 Person =   New Person ();
4 Swap ( Ref Person );
5

 

The copy process is absent when passing the parameter. Therefore, if p = new person () is performed in SWAP, the value (address) of the real parameter is changed, then the original person instance will become a garbage collection waiting for GC.

 

I wrote a lot of words, and it is over now.

 

RelatedArticle: Note a reference passing parameter that I misunderstood

 

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.