Interview series pointer and reference usage scenarios

Posted Jun 15, 20203 min read

First throw two questions:

  • What is the difference between pointer and reference?
  • When should I use a pointer? When should I use references?

The difference between pointers and references

First look at a piece of code:

int a = 1;
int b = 2;
int *c = nullptr;
c = &a;
int &d = b;

A pointer is actually an integer that stores a memory address. This integer represents the address of the variable being pointed to.

The reference is actually an alias of the variable, that is, to give the variable a new name. Note that since the reference is an individual name, then it must have an ontology, a person named Wang Erxiao, we can also give it an individual name two Egg, we mentioned that the two eggs and Wang Erxiao are actually the same person.

the difference:

  • The pointer can be temporarily uninitialized when it is declared, that is, pointer = nullptr, the pointer may be a null pointer at any time during the life cycle, so you should check it every time you use it to prevent the null pointer exception problem, but the reference does not need Check, because the reference is never empty, it must have an ontology, it must represent an object, and the reference must be initialized at the same time it is created.

      void FuncPtr(int *ptr) {
          if(ptr != nullptr) {
              cout << *ptr;
          }
      }
    
      void FuncReference(int &ref) {
          cout << ref;
      }
  • The pointer stores the address, the pointer can be reassigned, it can point to an object during initialization, and it can also point to another object at other times, and the reference is very specific, it will end up from one, it always Refers to the object it originally represented. To give another example, there is a man named Trump who gave him a citation alias called the most stupid president in history. This quote will definitely end, that is, whenever the most stupid president in history must be Trump. The US President can be used as a pointer. It can be Obama for a while, Trump for a while, or nullptr for a while.

Usage scenarios for pointers and references

The main function of the reference is as a function parameter and return value, look at a piece of code:

struct A {
    int a;
};
void func(const A &a) {
    cout << a.a;
}

vector<int> vec(10);
vec[3]= 3;

Why can the value of the vector container be changed by vec[3]=3, because the []operator returns a reference, which is equivalent to an alias for the internal variable. Here, you can also use the []operator Return a pointer, that is, *vec[3]=3, is this a bit ugly, and does not meet the grammatical requirements.

In fact, we may also notice during the programming process that in fact pointers that can do anything that can be done by reference can be done, but why should we still quote this thing?

Answer:Do the right job with the right tools. Pointers can operate anything in memory without constraints. The function is very powerful, but it is also very dangerous, so you can use references at the right time. When you need to point to something, And it must be specific, never let it point to other things. For example, some function parameters can use references to avoid copying, or implement an operator whose syntax requirements cannot be achieved by pointers, such as vec[3]=3. Use references, and use pointers at all other times.

There are actually a lot of knowledge points after c++11. For details, please see my previous article:

References

"More Effective C++"

https://www.runoob.com/w3cnot...

https://blog.csdn.net/weikang...

https://www.jianshu.com/p/e54...

https://zhuanlan.zhihu.com/p/...
For more articles, please pay attention to my V X nickname:Lord Meow, welcome to communicate.