Some knowledge points about c++ declaration

"C++ Programming Language (Fourth Edition)" Chapter 6.3

1. The memory space is not allocated for the variable when it is declared, and the memory space is allocated when the variable is defined.

2. The same variable can be declared multiple times, but only once.

3. Use the scope resolution operator:: to access the shielded global variables:

int x = 666;
int main(int argc, char *argv[])
{
    int x = 3;
    debug x << ::x;
}

4. It is recommended to use the following form when not using auto to define variables:

int i {666};

Use when using auto:

auto i = 666;

Because auto i{666}; inferred is the initializer_list<int> type

5. When the variable is defined but not initialized:

  • If it is a static variable, the global variable will be initialized by default. (Int i; is equivalent to int i{}; the value of i is 0)
  • Local variables and variables on the heap memory:
  1. When in the default constructor of a custom type , perform default initialization
  2. Otherwise, no initialization is performed, and its value is uncertain

6. When auto infers, it will implicitly dereference

auto x = v;//x的类型为int而不是int&
auto & y = v;//y的类型是int&

Guess you like

Origin blog.csdn.net/kenfan1647/article/details/113807652