Introduction to References
What is a reference? A reference is an alias, and an alias is another name that creates an object. Create a referenced method with the & symbol. In C + + You can think of it as another kind of pointer, by reference type We can also indirectly manipulate the object, the reference type is mainly used in the formal parameters of the function, usually we use it is to pass the class object to a function. Reference objects are defined by the type name plus the & symbol and name. For example:(int &test;), here we define a reference to the int type named test, but int &test; is not able to be compiled successfully, Because the definition of the reference must be assigned to the application at the same time, the assignment here is not to pass the value of the variable to the reference, but to point the reference to the variable, written so that:(int &test= variable name ;).
Example one:
int_tmain (intARGC, _tchar*argv[]) { intA =Ten; int&b =A; b= B +2; cout<<&a<<"|"<<&b<<"|"<<a<<"|"<<b; GetChar (); return 0;}
The output is:
As you can tell from the above results, the addresses of A and B are the same, and the results are all 12.
What about the reference as a function of the parameter?
Example Two
#include<iostream>using namespacestd;//---------------------------------------------------------------------------voidSwapint&a,int&b);int_tmain (intARGC, _tchar*argv[]) { intx, y; X=5; Y=Ten; cout<<"x= in main function"<<x<<"y="<<y<<Endl; Swap (x, y); cout<<"call function swap in x="<<x<<"y="<<y; GetChar (); return 0;}voidSwapint&a,int&b) { inttemp; Temp=A; A=b; b=temp;}
From the above results analysis, the function of reference and pointer is the same.
Frequently cited
A reference that is declared with a const is a constant reference. the object referenced by the common reference cannot be changed. What we often see is a formal parameter that is often referenced as a function, so that false modifications to the arguments do not occur. The commonly referenced declaration form is: const type descriptor & reference name. An example of a regular reference as a function parameter:
#include <iostream>using namespacestd;voidShowConst Double&R);intMain () {DoubleD9.5); Show (d); return 0;}voidShowConst Double&R)
//normally referenced as a parameter, the object referenced by R cannot be updated in the function. {cout<<r<<Endl; }
Constant Object
The so-called constant object, refers to the data members in its lifetime will not be changed. A constant object must be initialized when it is defined, and the value of its data member cannot be changed. a common object is declared as a class name, a const object name, or a const class name object name. Examples of common objects are as follows:
Classpublic: A (int i,int j) {x=i; y=J;} Private intconst A (6,8//A is a constant object and cannot be updated
If a statement that modifies the data members of a constant object appears in the program, the compiler will make an error. In general, there are two ways to modify the data members of an object, one is to access the public data member through the object name and modify its value, and the data members of the constant object cannot be modified, the other is that the member function of the class modifies the value of the data member, and the regular object cannot call the normal member function. However, in this case, the constant object is only the data, there is no external interface, which requires a constant object specifically defined by the constant member function.
the constant member function of a class
A member function declared with a const in a class is a regular member function. The usual member function is declared as: type specifier function name (parameter table) const;.
To remind you of the points: a. Constant member functions are declared and implemented with the const keyword ; b. The constant member function cannot modify the object's data member, nor can it access the class without const Declaration of a very member function ; c. A regular object can only invoke its regular member function, and cannot call other ordinary member functions ; d. Const keywords can be used to participate in the separation of overloaded functions , for example, if there are two such declared functions: void Fun (); void fun () const;, then they are overloaded functions.
#include <iostream>
using namespace Std;
Class R
{
Public
R (int r1, int r2) {r1=r1; R2=R2; }
void print ();
void print () const;
Private
int r1,r2;
};
void R::p rint ()
{
cout<<r1<< ":" <<R2<<endl;
}
void R::p rint () const
{
cout<<r1<< ";" <<R2<<endl;
}
int main ()
{
R A (5,4);
A.print (); Call Voidprint ()
Const R B (20,52);
B.print (); Call Voidprint () const
return 0;
}
The above R class declares two function print with the same name, and the second is a regular member function. Two objects A and b,b are defined in the main function, which is called by a function that is not declared with const, whereas a const-declared constant member function is called by B.
constant data member of the class
A data member of a class can also be a constant and a regular reference, and a data member declared with a const is a regular data member. You cannot assign a value to a constant data member in any function . Constructors initialize a regular data member only by initializing the list .
#include <iostream>
using namespace Std;
Class A
{
Public
A (Inti);
void print ();
Const int& R;
Private
const int A;
static const int B; Static constant data members
};
const int A::B=20;
a::a (int i): A (i), R (a) {}
void A::p rint ()
{
cout<<a<< ":" <<b<< ":" <<r<<endl;
}
int main ()
{
The object A1 and A2 are established, and the constructors are called with 50 and 10 as the initial values, and the initialization list of constructors is used to assign the initial values to the object's constant data members.
A A1 (A), A2 (10);
A1.print ();
A2.print ();
return 0;
}
The result of this program is:
50:20:50
10:20:10
C + + 's tenth lesson citation