Detail the differences between Pointers and references in C++

  • 2020-06-15 10:04:44
  • OfStack

1. The nature of Pointers and references

(1) The pointer is a variable that stores the memory address, and the special place is that it stores the memory address. Therefore, the pointer size does not change as other variables 1, only the current platform - different platform memory address range is not the same, 32 bit platform, the maximum memory is 4GB, so only 32bit can be stored, so the size of sizeof (pointer) is 4 bytes. On a 64-bit platform, 32 bits are not enough, and 64 ES7en is needed to represent all the memory addresses. So sizeof is 8.
(2) The essence of reference is "variable alias", which means to give a variable another name. Since it is "alias", 1 must have an ontology.

2. The difference between declaration and initialization

The pointer points to 1 memory address, so it can point to an address with block 0x00000000. It can be declared without initializing temporarily (not recommended), that is, pointer = NULL;

A reference is an alias for a variable; the alias name corresponds to a "real name" for each one, so it must be initialized at declaration time and cannot be initialized to null.

3. Differences in usage

(1) According to the difference between the two in declaration and initialization, the pointer may be Null at any time in the declaration period, so the use of 1 must be checked to prevent the occurrence of null pointer and wild pointer; References don't have to worry about that anymore, just initialize it, and you can use it anywhere, and you don't have to worry about it being empty or anything.
(2) Since the pointer stores itself as a memory address, since it can store the initialized (or assigned) address, it can store other addresses during the lifetime of the pointer, as long as you are a variable of the same type (the corresponding type offset is not 1 for different types), OK for the pointer.

A reference is an alias for a variable AA that can only "end from 1" throughout its life cycle, always the alias for the variable that initialized it the first time, during which any operation on it is the same as an operation on the variable AA.

Talk is cheap,show you my code.


/**  Examples of Pointers and references  **/

  std::string s1 = " radish ";
  std::string s2 = " Green vegetables ";
  std::string s3 = " egg ";
  std::string s4 = " tomatoes ";
  /**  Pointers can be initialized to null  **/
  std::string *p_Str = NULL;
  /**  reference 1 Must be initialized at the beginning  **/
  std::string& r_Str = s1;

  p_Str = &s2;
  std::cout<<" I am a pointer "<<*p_Str<<std::endl; /**  Green vegetables  **/
  std::cout<<" I am a reference "<<r_Str<<std::endl; /**  radish  **/
  std::cout<<std::endl;
  std::cout<<"********* Modify the pointer and reference, respectively ***********"<<std::endl;
  /**  Modify the pointer and reference, respectively  **/
  r_Str = s3; /**  Try to make the r_Str for s3 The alias  **/
  p_Str = &s4; /** p_Str Repoint s4 **/
  
  std::cout<<" I am a pointer "<<*p_Str<<std::endl; /**  tomatoes  **/
  std::cout<<" I am a reference "<<r_Str<<std::endl;  /**  egg  **/
  std::cout<<std::endl;
  std::cout<<"********* Look at the impact of the changes just made on the initial initialization ***********"<<std::endl;
  /**  Looks like it worked. It's all been modified as intended, but wait a minute  **/
  std::cout<<" I am a s1"<<s1<<std::endl; /**  egg   !!!!!!!!! Pay attention to   !!!!!!!!!  **/
  std::cout<<" I am a s2"<<s2<<std::endl; /**  Green vegetables  **/
  std::cout<<" I am a s3"<<s3<<std::endl; /**  egg  **/
  std::cout<<" I am a s4"<<s4<<std::endl; /**  tomatoes  **/

  /** 
   found s1 " radish "  It becomes the sum s31 The sample of " Green vegetables " , which also means that any operation on a reference is equivalent to the operation on the original variable itself 
   In contrast, Pointers have a high degree of freedom, they can be pointed to whoever they want, and it doesn't affect any variables that were previously pointed to 
   No surprise, no surprise  :)
  **/

4, summarize

A less apt metaphor is that the pointer is like a "cheating philandering man" who can (yes, but not necessarily) get around and point to any address at any time. A quote is like an "honest person" who can only "end with one (who initializes it)."

Also, according to Scott Meyers in More Effective C++, you should only use references if you are sure you need 1 to start and do not need to point to other types, otherwise you should always use Pointers.

Little brother know vulgar shallow, have improper place, please big guy pat.


Related articles: