Clear, practical technology insights About · Contact

Reference in C ++: Explained

Understand Reference in C ++ with clear explanations, practical examples, and useful tips. This updated guide covers the essential concepts and common mistakes.

Published: 3 minutes read
Table of Contents

Reference in C ++ is easier to understand when the core ideas are paired with practical examples. The sections below explain the topic clearly, highlight useful steps, and point out details that can prevent common errors.

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 help 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 example below:

 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 example below 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 Description

Pass parameters by reference in C ++

C ++ supports passing references as function parameters that are safer than parameters

Returns the value by reference in C ++

You can return a reference from a C ++ function like any other data type that can be returned

Pass 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 } 

Next lesson: Date and Time in C ++

Frequently Asked Questions

What should you know about 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 ++:

What should you know about 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.

What should you know about pass 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 ++.

Was this article helpful?

Your feedback helps us improve.

Discussion

Reader Comments 0

Sign in with email or Google to join the discussion.