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:

Reference in C ++ Picture 1

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:

Reference in C ++ Picture 2

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:

Reference in C ++ Picture 3

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 ++

5 ★ | 1 Vote

May be interested

  • Date and Time in C ++Photo of Date and Time in C ++
    the c ++ standard library (c ++ standard library) does not provide an appropriate date type. c ++ inherits the structure and function to manipulate date and time from c. to access functions and structures related to date and time, you will need to declare in your c ++ program.
  • Input / Output in C ++Photo of Input / Output in C ++
    the c ++ standard library provides many possibilities for input / output and will be discussed in later chapters. in this chapter, we discuss the most basic and popular i / o operations required for c ++ programming.
  • Data structure in C / C ++Photo of Data structure in C / C ++
    struct in c / c ++ arrays in c / c ++ allow you to define several types of variables that can hold the values ​​of several members of the same data type. but the structure is another type of data in the c / c ++ programming language, which allows you to combine other types of data.
  • Class (class) and Object in C ++Photo of Class (class) and Object in C ++
    the main purpose of c ++ is to add object orientation to the c programming language and classes which are central features of c ++ that support object-oriented programming and are often called types user defined (user-defined).
  • Class member functions in C ++Photo of Class member functions in C ++
    a member function of a class is a function that has its definition or prototype inside the class definition like any other variable. it works on any object of the class that is a member, and has access to all members of a class for that object.
  • Access Modifier for class in C ++Photo of Access Modifier for class in C ++
    data hiding is one of the important features of object-oriented programming that allows to prevent the function of a program from directly accessing the internal representation of a class type.