(Migration) c ++ references

Source: Internet
Author: User

(I Accidentally flipped through my old blog and suddenly found this article I wrote in my junior year. I thought it was still the case, so I quickly got it out)

Declaration of reference: Basic Format: Reference Type & reference name = referenced object

  1. & Operator: The declaration Operator & has nothing to do with the get address operator & bitwise OR operator &
  2. Extern Keyword: Generally, the referenced declaration must specify that the only exception to the referenced object is the use of the extern keyword.
  3. Const Keyword: Generally, the referenced object must be a valid left-value object, but the constant reference type modified by the const keyword allows the referenced object to be a non-left-value object.

Reference: A reference can be used as an alias of a referenced object. A simple reference is meaningless. It is mainly used in four aspects:

  1. Function parameters: modifiable real parameters, efficient and complex object transmission methods
  2. Function return value: returns a function of the Left value type without generating a copy return value.
  3. Operator Overloading: provides a method that is closer to the original meaning for ++ -- <> and other operators.
  4. Polymorphism: used instead of pointer to reference abstract classes

In-depth discussion of references: start now

1. Does the reference occupy the memory space?

If the reference is just an alias, it should not occupy the memory space. I will use the following code to view it.

 

Code

# Include <iostream>
Using namespace std;
Void fa (){
Int a [4];
Cout <a <endl;
}
Void fb (){
Int a [4];
Int & B = a [0];
Cout <a <endl;
Fa ();
}
Void fc (){
Int a [4];
Cout <a <endl;
Fb ();
}
Int main ()
{
Fc ();
Getchar ();
Return 0;
}

 

 

In this Code, I used three nested function calls so that the fa and fc stack segments will change the size of the fb stack segments in the middle, which will lead to the address offset of a in fc. however, not every allocation will lead to an increase in the stack segment. Therefore, we should first test to determine the proper size of a so that the stack segment of variable B will increase once declared.

Set int & B = a [0]; if you comment out, we can see that the address output in fc has changed. If fb has not changed, referencing B obviously occupies the memory space. My code is compiled in g ++, and the result is also the result of g ++.

2. Can the referenced value be changed?

The object referenced by an initialized reference type object cannot be changed legally.

The previous Code shows that the reference actually occupies the memory space. To learn more about its nature, we must obtain its address. & The address fetch operator is obviously impossible. Almost everyone who learns c ++ will try to use this method to obtain the address of the reference type, but all the results will be the address of the referenced object.

In addition, starting from the previous example, if the address of a in fb does not change the address of a in fa, the allocation of int and B should be between the two, so the most likely location is a in a [4] But after the output, I found that a [4] is not, because array a is reversely allocated to the stack, I tried this code.
Void fb (){
Int a [4];
Int B = 20;
Cout <a [-1] <endl;
Fa ();
}

In my compiler, B and a [-1] are always equal, so I replace int B with int & B = a [0]; OK. I found that it points to something that looks like an address. Change B's point and find that a [-1] is changing. Now I can almost make sure that a [-1] is B and then use such code to test it.
Void fb (){
Int a [4] = {1, 2, 4 };
Int & B = a [0];
A [-1] + = 4;
Cout <B <"," <a [0] <endl;
Fa ();
}

Is the reference really unchangeable? In this example, B points to a [1] instead of a [0].

3. References and pointers
From the example above, we can see that the referenced internal implementation is no different from the pointer. If you refer to the ideas of other languages, you can draw a conclusion that reference is a pointer constant. References in c ++ are obviously different from pointers in syntax, but they are not essentially different. References are a type of restricted constant pointers implemented in c ++. it is automatically referenced before any operation.

We recommend that you replace pointers with references whenever possible, because references are a safer type than pointers and have clearer semantics (of course, pointers also have appropriate semantics)

4. Other Languages
By the way, references in other languages of the C family are almost the only way to access objects.

C ++: if you do not want to copy any object without passing parameters and return values as references, you must specify the parameter as the reference type.
C #: In the unsafe mode, operators except the = Operator will unreference all objects as references. When passing parameters and return values, if you want to copy them, you must explicitly clone them.
Java: The operator without a pointer except = Will unreference similar to c #
Javascript: no pointer, but the operators except = can solve the problem of referencing methods that do not provide copy transfer (depressing)

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.