const, static keyword, extern "C" usage

static

Consider the case of the class

static member variable

  • Only associated with the class, not associated with the objects of the class, there is only one copy of all objects of the class
  • Allocate space at definition time
  • Cannot be initialized in the class declaration, must be initialized outside the class definition
  • It does not need to be marked as static during initialization, and can be arbitrarily accessed by non-static member functions
  • Why do static member variables of a class need to be initialized outside the class definition?
  1. Static member variables are the attributes of the class and have nothing to do with any specific object instance, so the memory has been allocated when the program starts. If you initialize the static member variables in the class definition, it is equivalent to defining the class for it It is unreasonable to allocate memory space and initialize it. (static modified variables exist before the object)
  2. If the initialization of static member variables is placed in the class definition, then each file that includes the header file will have a definition of static member variables, so there will be errors of repeated definitions.

static member function

  • does not have a this pointer

Because the class members modified by static belong to the class and not to the object, there is no this pointer, so the static class member functions cannot access non-static class members

  • Non-static member variables and non-static member functions of class objects cannot be accessed
  • cannot be declared const, virtual and volatile

The role of the const member function is not to change the state of the object, but the static member function does not belong to any specific object, meaningless and not allowed; the virtual function needs to use runtime type information to choose which function to call, but the static function is not related to any object Association, so objects that do not belong to the class have no runtime type information to choose from (there is another reason, static member functions do not have this pointer, and the implementation of virtual functions is to assign a virtual table pointer to each object instead ofThe virtual table pointer is called through the this pointer, so virtual cannot be used); the volatile keyword is used to indicate that variables may be accidentally modified, thus telling the compiler that certain optimizations are not required, but static member functions do not belong to objects and do not involve any variables (only static member variables and other static member functions), there is no need to declare it as volatile.

  • It can be accessed arbitrarily by non-static member functions.

The difference between static members and ordinary members

life cycle

Static member variables exist from the time the class is loaded until the class is unloaded. Ordinary member variables only begin to exist after the object is created in the class, and the life cycle ends when the object ends.

Class loading and unloading: Classes are dynamically loaded when the program is running.
Class loading usually occurs in the following situations :

  • When creating an object of a class: When the program executes a statement that creates an object of a class, the compiler will allocate space for the object in memory and initialize it according to the definition of the class.
  • When calling a static member or static function of a class: The static member is loaded into memory when the program starts, and exists throughout the execution of the program.
  • When calling a member function of a class: The definition of the member function is usually in the declaration of the class, but the actual function code will not be loaded when the class is loaded, but will be loaded when the function is called.

Unloading of classes occurs when :

  • When an object's lifetime ends: when an object goes out of scope, or when dynamically allocated memory is released via the delete keyword, the object is destroyed and unloaded from memory.
  • When the program terminates: At the end of the program, all loaded classes and objects are unloaded and memory is freed.

The loading and unloading of classes is managed automatically by the compiler and the operating system.

sharing method

Static variables are shared by the whole class, and ordinary member variables are shared by each object separately.

define location

Ordinary member variables are stored in the heap or stack, while static member variables are stored in the static global area.

initial position

Ordinary members are initialized in the class, static member variables are initialized outside the class

default argument

You can use static member variables as default arguments

regardless of class

hide

Can only be used in the same compiled module as this file

default initialization 0

Including uninitialized global static variables and local static variables, there is a global uninitialized area [this area is not allocated memory in the compiled object file (because the default is 0 and does not need to store a large number of zero values), it is only required for recording Size, it is located between the data segment and the stack area]. In fact, global variables also have this property, because global variables are also stored in the static data area

Static variables are defined inside a function and always exist

You can control the scope by defining it inside the function.It only needs to be initialized once (initialization is completed when the program starts to run), has memory, and has the same scope as local variables. It still exists after the function exits, but it cannot be used
A total of two kinds of variables are stored in the static storage area: global variables and static variables, but static can control the visible range of variables.

When are static variables initialized

  • Before the main program, the compiler has allocated memory for it. Static local variables are stored in the global area like global variables, so the compiler has allocated memory for them before the main program.
  • The initialization nodes of static local variables in C and C++ are different:
    • In the C language, initialization occurs before the code is executed, and initialization occurs after the memory is allocated in the compilation phase. Therefore, variables cannot be used to initialize static local variables in the C language. When the program ends, the global memory where the variables are located will be recycled.
    • In C++, when initialization is executedThe relevant code will only be initialized when the, because C++ introduces the concept of class, it is often necessary to perform some specific operations instead of simply allocating memory in constructors and destructors. Therefore, the C++ standard positioning global or static objects will only be constructed when they are used for the first time, and managed through atexit(). At the end of the program, the destruction is performed in the reverse direction of the construction order.So C++ Chinese can use variables to initialize static local variables

The difference between global variables and static variables

  • Global variables themselves are static storage methods, and static global variables are also static storage methods. The scope of non-static global variables is the entire source program. When a source program is composed of multiple source files, non-static global variables are valid in each source file.Static global variables limit the scope and are only valid within the source file in which the variable is defined.
  • The difference between static global variables and ordinary global variables is that static global variables are only initialized once to prevent them from being referenced in other file units.

const

regardless of class

  • Initialized when defined, cannot be modified later
  • The const parameter can receive both const and non-const type parameters
  • In a function declaration, const can modify the formal parameter to indicate that it is an input parameter and its value cannot be changed inside the function
  • A const type variable can be converted from a const type to a non-const type through the type conversion operator const_cast

consider class

const member variable

  • It cannot be initialized outside the class definition, it can only be initialized through the constructor initialization list, and there must be a constructor
  • Different classes can have different values ​​for their const data members, so they cannot be initialized when they are declared in the class
  • const member variables also have a hidden effect
  • The const type variable must be initialized when it is defined, so if the member variable of the class has a const type variable, then the variable must be initialized in the initialization list of the class
  • Only reference passing and pointer passing can be overloaded with whether to add const.

The compiler determines which function to call by matching the function name and argument list during compilation. However, top-level const is transparent to the compiler, that is, the compiler cannot distinguish between a formal parameter with top-level const and another parameter without top-level const during function selection. So if there are two function definitions, one with a top-level const parameter and one without a top-level const parameter, the compiler won't be able to distinguish between them, resulting in a function overload conflict.
However, the situation is different when the formal parameters are passed by reference or by pointer. In this case, the compiler can choose the correct function based on whether the actual parameter has an underlying const or not. Because both pointer passing and reference passing can modify the pointed object, it makes sense to distinguish formal parameters by whether to add underlying const in function overloading

const member function

  • A const object cannot call a non-const member function (because a member function that is not explicitly declared as const is regarded as a function that will modify the data members in the object, and the compiler does not allow it to be called by a const object), non-const objects can call
  • Cannot change the value of non-mutable data
  • For a member function of a class, if it is specified as a const type, it indicates that it is aconstant function, You cannot modify the member variables of the class, and the constant objects of the class can only access the constant member functions of the class.
  • For member functions of a class, sometimes the return value must be specified as a const type, so that its return value is not an lvalue
  • A const member function can access non-const data members and const data members of a non-const object, and can also access all data members in a cosnt object; a non-const member function can access non-const data members and const data members of a non-const object, but not can visitAny data member of a const object

Top-level const and bottom-level const

  • top-level const: constThe modified variable is a constant (referring to a pointer), const on the right of *
  • underlying const:The object referred to by the variable modified by const is a constant (referring to the referred variable), const on the left of *
  • A constant underlying const cannot be assigned to a non-const underlying const
  • When using the named coercion function const_cast, only the underlying const of the operand can be changed

extern “C”

After adding extern "C", it is equivalent to telling the program that this code is written in C language, so it is compiled according to C language. But this method cannot be used in C language.

  • C++ calls C function
//xx.h
extern int add(...)

//xx.c
int add(){
    
    
    
}

//xx.cpp
extern "C" {
    
    
    #include "xx.h"
}

  • C calls C++
//xx.h
extern "C"{
    
    
    int add();
}
//xx.cpp
int add(){
    
        
}
//xx.c
extern int add();

Guess you like

Origin blog.csdn.net/qaaaaaaz/article/details/130734680