directory

  • Define, const
  • static

Define, const

In C++, both const and define can be used to define constants. But there’s a big difference:

  • Define the role of

    A constant defined with define has no type. The compiler simply associates the value of the defined constant with the name of the constant. During compilation, the compiler simply replaces the name of the constant with the corresponding value.

  • Const role

    Constants defined using const are typed and stored in a static region of memory.

Specifically, the two have the following differences:

  1. A constant defined with or without a pointer to define cannot be used to point to the value because it is simply a substitution

Constants defined by const can have Pointers to the address of the value

  1. Can you define functions? You can define some simple functions with define, but const doesn’t

  2. Compiler handling

    Replace the define preprocessing stage

    Const compile-phase replacement

  3. Type checking

    Define does not check the type

    Const checks the variable type

  4. Memory space

    Constants defined by define do not occupy memory space

    A constant defined by const occupies the storage space of a static region and only occupies one copy

  5. other

    At compile time, the compiler usually does not allocate storage for const variables. Instead, it saves memory in the symbol table, which makes it a constant at compile time without frequent memory reads and writes

  6. scope

    The define macro defines scope only in the current file

    The scope of a const definition is only in the current file. When a const variable of the same name appears in different files, we define different variables. We must also add the extern keyword before the definition if we want const variables to be shared across multiple files

    Some properties of const

    Use const constants instead of macro constants in C++

  7. The role of const in a class

If you want to share a constant in a class, you would naturally want to use const constants. But this is not true, because const constants are constant only for the lifetime of the object, but are mutable for the entire class. Because multiple objects can be created in a class, and const members of different objects can have different values. A const data member cannot be initialized in a class declaration. A const member variable can only be initialized in a constructor

static

Static static variable scope only within a file, the program at the start of the allocated space, the end of the program release space, the default initialization value is 0, in use process can modify the value of the static variables, static variables and static function and it can only see the code in this document, the name is not visible in the other files.

  1. Declare static variables inside functions. Static variables can be used as a communication mechanism between objects

If a local variable is declared static, there will be only one statically allocated object that is used to represent the variable in all calls to the function. This object is initialized when its definition is reached the first time the thread in which it is executed. When compiling multiple files at the same time, all global variables and functions that are not static are globally visible; if static is added, they are hidden from other files.

  1. Local static object

For local static objects, the constructor is called the first time the control thread passes the definition of the object. At the end of the program, the destructors for local static objects are called one by one in the reverse order in which they were constructed, without specifying an exact time. Variables stored in the static data area are initialized, and only once, when the program starts to run. There are two types of variables stored in static storage: global variables and static variables, although static, in contrast to global variables, controls the visibility of variables. Static is, after all, used to hide.

  1. Static members and static member functions

A variable is called a static member if it is part of a class but not part of the objects of that class. A static member has only one copy, unlike regular non-static members who have one copy per object. Similarly, a function that calls a class member without calling it on a specific object is called a static member function. Static member functions of a class can access only static members (variables or functions) of the class. A third function of static is that it is initialized to 0 by default. Global variables also have this property because they are also stored in static data.