Talking about the usage of const

Article Directory

Usage of const

const int* p1 = &a;

  What is protected is the content pointed to by the pointer. It cannot be changed when p1 is accessed, but it can be changed from a.

int const* p2 = &a;

  Like the first one, the content pointed to by the pointer is protected. It cannot be changed when p1 is accessed, but it can be changed from a.

int* const p3 = &a;

  The pointer variable itself is protected, and the contents pointed to cannot be changed.

Summary
  const protects the content pointed to by the pointer before, and const protects the pointer afterwards.

Guess you like

Origin blog.csdn.net/weixin_43580319/article/details/111509425