C ++ constant reference correct application method

Source: Internet
Author: User

In C ++ programming languages, constant reference is a very important application technique. In this article, we will introduce in detail the application methods of C ++ constant reference. I believe you can learn a lot from it.

  • Basic Content of C ++ Singleton Mode
  • Analysis of related methods for creating Web services in C ++
  • How to initialize the C ++ Constructor
  • Skills related to C ++ identifier naming rules
  • Basic concepts of C ++ function templates

If a constant is referenced, the compiler first creates a temporary variable and then places the value of the constant into the temporary variable. The referenced operation is the operation on the temporary variable. The C ++ constant reference can be initialized with any other reference, but cannot be changed.

There are two points worth noting about the initialization of the reference:

1) there is no problem when the initialization value is a left value and the address can be obtained;

2) When the initialization value is not a left value, only one const T & constant reference can be assigned a value. In addition, this assignment has a process:

First, the value is implicitly converted to the type T, and then the conversion result is stored in a temporary object. Finally, this temporary object is used to initialize the reference variable.

Example:

 
 
  1. Double & dr = 1; // error: The left value is required.
  2. Const double & cdr = 1; // OK

The actual process of the second sentence is as follows:

 
 
  1. double temp = double(1);  
  2. const double& cdr = temp; 

When using function parameters:

 
 
  1. // bc_temp_objects_not_bound_to_nonconst_ref.cpp  
  2. // compile with: /EHsc  
  3. #include "iostream"  
  4. using namespace std;  
  5. class C {};  
  6. void f(C & c) { cout << "C&" << endl; }  
  7. void f(C const & c) { cout << "C const &" << endl; }  
  8. int main() {  
  9. f(C());  

Result:

C const &

More directly, use the basic types:

 
 
  1. #include <iostream> 
  2. using namespace std;  
  3. void display(int const &ref) {cout<<ref<<'\n';}  
  4. int main()  
  5. {  
  6. int i=1;  
  7. display(i);  
  8. int const anotheri=2;  
  9. display(anotheri);  
  10. display(2);  
  11. display(1+2);  
  12. display(static_cast<int>(3.14159));  

Return a local object from the function through the C ++ constant reference:

Generally, it is incorrect to return a local object reference from a function:
 

 
 
  1. T & my_op ( void )   
  2. {   
  3. T t;   
  4. return t;   
  5. } // The T object t got destroyed here so the 
    returned reference is not valid anymore.  

Special case: a common reference is returned.

 
 
  1. const T & my_op ( void )   
  2. {   
  3. T t;   
  4. return t;   
  5. }   
  6. const T & my_t_obj = my_op ();  

In this case, the local variable t will not be directly analyzed, but will be retained until the end of the lifecycle of my_t_obj.

In short, a temporary variable can be referenced in the C ++ constant reference syntax. This method makes sense when using references as function parameters and returning local variables. In my opinion, constant reference is mainly used for function parameters or to ensure that the original variables are not modified.

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.