Reference in C ++
A reference variable is an alias, which is another name for an existing variable. When a reference is initialized with a variable, then either the variable name or the reference name can be used to reference that variable.
Reference and pointer in C ++
Using references can often confuse you as with pointers, however, there are 3 different points between reference and pointer in C ++:
You cannot have NULL reference. You must always assume that a reference is connected to a part of the repository.
When a reference is initialized to an object, it cannot be changed to reference another object. Pointers can be pointed to another object at any time.
A reference must be initialized when it is created. Cursors can be created at any time.
Create references in C ++
You consider a variable name as a label attached to a variable location in memory. You can then think of the reference as the second label attached to that memory location. Therefore, you can access the content of the variable through either the original or reference variable name. Suppose we have the following example:
int i = 19 ;
You can declare reference variables for i as follows:
int & r = i ;
Read & in these statements are Reference . Therefore, in the first declaration, r is an integer reference initialized to i and in the second reference, s is a double reference initialized for d. The following example uses references in C ++:
#include using namespace std ; int main () { // khai bao cac bien int i ; double d ; // khai bao cac bien tham chieu int & r = i ; double & s = d ; i = 5 ; cout << "Gia tri cua i la: " << i << endl ; cout << "Gia tri cua tham chieu toi i la: " << r << endl ; d = 11.7 ; cout << "Gia tri cua d : " << d << endl ; cout << "Gia tri cua tham chieu toi d la: " << s << endl ; return 0 ; }
Running the above C ++ program will produce the following results:
C ++ references are often used for a list of function and function parameters that return values. Here are two important concepts related to references in C ++, you will see details at the end of the article:
Concept DescriptionPass parameters by reference in C ++
C ++ supports passing references as function parameters that are safer than parametersReturns the value by reference in C ++
You can return a reference from a C ++ function like any other data type that can be returnedPass parameters by reference in C ++
We discussed how we implement the concept of calling by reference by using pointers. Here is another example of calling by reference that uses reference in C ++.
#include using namespace std ; // khai bao ham void traodoi ( int & x , int & y ); int main () { // Khai bao bien cuc bo: int a = 100 ; int b = 200 ; cout << "Truoc khi trao doi, gia tri cua a la: " << a << endl ; cout << "Truoc khi trao doi, gia tri cua b la: " << b << endl ; /* goi mot ham de trao doi cac gia tri.*/ traodoi ( a , b ); cout << "Sau khi trao doi, gia tri cua a la: " << a << endl ; cout << "Sau khi trao doi, gia tri cua b la: " << b << endl ; return 0 ; } // phan dinh nghia ham de trao doi cac gia tri. void traodoi ( int & x , int & y ) { int temp ; temp = x ; /* luu tru gia tri tai dia chi x */ x = y ; /* dat gia tri y vao trong x */ y = temp ; /* dat gia tri x vao trong y */ return ; }
Running the above C ++ program will produce the following results:
Returns the value by reference in C ++
A C ++ program can be made to make it easier to read and maintain using references instead of pointers. A C ++ function can return a reference in the same way as it returns a pointer.
When a function returns a reference, it returns a hidden pointer to its return value. In this way, a function can be used in the left part of an assignment. You consider the following example:
#include #include using namespace std ; double mang [] = { 5.6 , 11.4 , 43.1 , 25.6 , 20.2 }; // phan dinh nghia ham double & tlGiaTri ( int i ) { return mang [ i ]; // tra ve mot tham chieu toi phan tu thu i } // ham main de goi phan dinh nghia ham tren. int main () { cout << "Gia tri truoc khi thay doi:" << endl ; for ( int i = 0 ; i < 5 ; i ++ ) { cout << "mang[" << i << "] = " ; cout << mang [ i ] << endl ; } tlGiaTri ( 1 ) = 12.44 ; // thay doi phan tu thu 2 tlGiaTri ( 3 ) = 25.0 ; // thay doi phan tu thu 4 cout << "Gia tri sau khi thay doi:" << endl ; for ( int i = 0 ; i < 5 ; i ++ ) { cout << "mang[" << i << "] = " ; cout << mang [ i ] << endl ; } return 0 ; }
Running the above C ++ program will produce the following results:
When returning a reference, you should be careful that the object being referenced is not out of scope. Therefore, it is invalid to return a reference to the local variable. But you can return a reference on a static variable.
int & func () { int q ; //! return q; // Lenh dang nay se cho mot Compile time error static int x ; return x ; // An toan, x ben ngoai pham vi nay }
According to Tutorialspoint
Previous article: Returns the pointer from the function in C ++
Next lesson: Date and Time in C ++
You should read it
- Delegate in C #
- Reference Database in MongoDB
- MS Word - Lesson 16: References and Citations
- How to create References - A list of references to standard references in Word
- Call the function by reference in C ++
- Complete guide to Excel 2016 (Part 15): Relative and absolute reference cells
- PHP functions
- Hardware in OSI reference model: Grade 5
- How to print website content on paper for reference
- Hardware in OSI reference model: Grade 4
- Hardware in OSI reference model: Grade 7
- The OFFSET function returns a reference in Excel
Maybe you are interested
How to use Structure Reference and Style Reference in Adobe Firefly
Quickly fix the Save with references to unsaved documents error
HTML ISO Country Code Reference
Reference Canvas in HTML
How to print website content on paper for reference
How to create References - A list of references to standard references in Word