About the "citation" thing.

Source: Internet
Author: User

1. The concept of references

What is a reference, popularly speaking, is the individual name (alias) for a target variable, which is exactly the same as the result of a reference operation and a target variable.

2. Declaration and use of references "issues needing attention

When declaring a reference, remember to initialize it. When the reference declaration is complete, the target variable has two names, the original name and reference name of the target variable, and the reference name cannot be used as an alias for the other variable. Declare a reference, not a new variable, it is just an alias of the target variable, it is not a data type in itself, so the reference itself does not account for the storage unit, and the system does not assign a storage unit to the reference. One more thing: You cannot create a reference to an array.

3. "References" as function arguments

1) The effect of passing a reference to a function is the same as passing a pointer. The parameter of the modulated function is called the real parametric in the original central melody function or an alias of the object, so the operation of the parameter variable in the modulated function is the operation of its corresponding target object (in the keynote function).

2) using the arguments of the reference transfer function, there is no copy of the actual argument generated in memory, it operates directly on the argument. In the case of a function call, the parameter must be assigned a storage unit, and the formal parameter variable is a copy of the argument variable. If the object is passed, the copy constructor of the class to which the object belongs is also called. Therefore, when the parameters pass large data, it is better to use a reference than the average variable to pass the parameter efficiency and occupy space.

3) Use pointers as arguments to functions, although they can also achieve the same effect as using references. However, it is also necessary to assign a storage unit to the parameter in the function, and to reuse the form of "* pointer variable name", which is prone to error and poor readability of the program; On the other hand, at the call point of the keynote function, the address of the variable must be used as the argument, and the reference is easier to use and clearer.

4. Use of "regular reference"

If you need to use references to improve the efficiency of the program, but also to protect the data passed to the function is not changed in the function, you should use a constant reference.

Common Reference declaration method:Const type identifier & reference name = target variable name;

EG1:

int A; Const int &ra=A;ra=1// error a=1//

EG2:

string foo (); void Bar (string & s); Bar (foo ()); // illegal bar ("helloWorld"// illegal

The reason is that both the Foo () and the "Hello World" strings produce a temporary object, whereas in C + + These temporary objects are const types. So the expression above is an attempt to convert an object of a const type to a non-const type, which is illegal.

Reference parameters should be defined as const when they can be defined as const.

5. "Reference" as the return value of a function

Follow the format:

Type identifier & function name (formal parameter list and type description) {       //}

The reference is used as the return value of the function to guarantee that a copy of the return value is not produced in memory.

Precautions:

1) cannot return a reference to a local variable . The main reason is that the local variable is destroyed after the function returns, so the returned reference becomes a "no reference", and the program enters an unknown state, resulting in runtime error!

2) You cannot return A reference to the memory that is allocated inside the function. Although there is no passive destruction of local variables, there are other embarrassing situations in which the case (a reference to new allocation of memory within the function) is returned. For example, a reference returned by a function is only present as a temporary variable, not given an actual variable, and the space pointed to by the reference (allocated by new) cannot be freed, resulting in memory leak.

3) You can return a reference to a class member, but preferably a const. The main reason is that when an object's property is associated with a business rule, its assignment is often related to the state of some other attribute or object, so it is necessary to encapsulate the assignment in a business rule. If other objects can get a very good reference (or pointer) to the property, the simple assignment of that property will break the integrity of the business rules.

4) The function of the return value of the stream operator overload declaration is "reference":

stream operator << and the >> , These two operators often want to be used continuously, for example: cout << "Hello" << Endl; Therefore, the return value of these two operators should be a stream reference that still supports both operators. Alternative options include returning a stream object and returning a pointer to a stream object. But for a stream object to be returned, the program must recreate (copy) a new stream object , that is to say, two consecutive << operators are actually for different objects! It's not acceptable to anyone. cannot be used continuously for returning a stream pointer << operator . Therefore, returning a Stream object reference is the only option. This unique choice is key, it shows the importance of the reference and the irreplaceable, perhaps this is the introduction of the concept of reference in the C + + language is the reason for it. Assignment operator =. This operation, like the Fuchang operator, can be used consecutively, for example: x = j = 10, or (x=10) = 100; The return value of the assignment operator must be an lvalue so that it can be assigned again. The reference is therefore the only return value selection for this operator.

#include <iostream>using namespacestd;int&put (intn);intvals[Ten];intError =-1;voidMain () {put (0) =Ten;//The value of the put (0) function as the left value is equivalent to vals[0]=10;Put9) = -;//The value of the put (9) function as the left value is equivalent to vals[9]=20;cout << vals[0]<<Endl; cout<< vals[9];}int&put (intN) {    if(N >=0&& N <=9)        returnVals[n]; Else{cout<<"Subscript Error"; returnerror; }}

Operation Result:

5) In some other operators, it is not possible to return a reference:+-*/ arithmetic character. They cannot return a reference , mainly because the four operators do not have side effect, so they must construct an object as the return value, and an optional scheme includes: Returning an object, returning a reference to a local variable, returning a reference to a new allocated object, Returns a static object reference. According to the preceding reference as the three rules for the return value, the 2nd and 32 scenarios are deprecated. A reference to a static object also causes an error because ((a+b) = = (C+d) is always true. So the only option left is to return an object.

6. What is the relationship between "referencing" and polymorphism?

A reference is another means of producing a polymorphic effect, in addition to pointers. This means that a reference to a base class can point to its derived class instance.

Class A; Class b:class a{...}; b b; Aref = b;

7. What is the difference between a " reference " and a pointer?

The pointer indirectly operates on the variable it points to after it points to an object through a pointer variable. The use of pointers in the program, the readability of the program, and the reference itself is the alias of the target variable, the operation of the reference is the operation of the target variable.

8. When do I need to " quote "?

Reference is recommended for the stream operator << and >>, the return value of the assignment operator =, the parameter of the copy constructor, the parameter of the assignment operator =, and other cases.

About the "citation" thing.

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.