Static member variables, member functions and ordinary member variables, memory relationship of member functions, this pointer, constant functions and constant objects

Posted by The.Pr0fess0r on Sat, 05 Mar 2022 13:53:58 +0100

Five memory areas of C + +

The memory pattern of C + + programs is usually divided into five areas: global data area, code area, stack area, heap area (i.e. free storage area) and constant area.
Global data area (static area): store global variables, static data and constants;
Code area: store the codes of all class member functions and non member functions, and the binary code of the function body.
Stack area: store the local variables, function parameters, return data, return address, etc. allocated for running the function.
Heap area: the memory block allocated by new, malloc and realloc. Generally, the compiler will not release the memory and needs to release it by program. Memory leakage is usually referred to as heap area.
For example, the constant storage area is not allowed to be modified.

Static member variables, member functions and ordinary member variables, memory allocation of member functions

Static member variable: it is stored in the global data area, so it is also called object shared data
Static member function: the code area of memory belongs to a class rather than an object, and does not occupy the storage space of the object
Common member variable: allocate memory with the establishment of objects. Different objects are stored in different areas. Global objects and static local objects are stored in the global data area, local objects are stored in the stack area, and dynamic objects are stored in the heap area
Ordinary member function: stored in the code area, it belongs to a class and does not occupy the storage space of the object

As shown in Figure 1.1

 

Static member variables and functions

life cycle

The life cycle of static member variable is the whole program, and the scope is in the class, while the life cycle of static variable is the whole program, and the scope is in the function body. When its value is called for the first time, its value will remain unchanged, and the next operation is still the value calculated after the first call.

definition

Static member variables must be declared inside the class and initialized outside the class.

class Maker
{
public:
    Maker()
    {
        //a = 20;
    }
public:
    static int a;
};
Initialization code:
int Maker::a = 100;

Static member variables belong to a class, not an object

Static member variables belong to classes and not objects. They are shared by all objects. There is only one variable for ten objects, saving space and time. Reason: static member variables are stored in the global data area and ordinary member variables are stored in the same area. All objects share their contents, so you modify the contents of static member variables in one object. When other functions call this variable, it is still the modified value of the previous object. However, this does not mean that, Static member variables and ordinary member functions are stored in the same location, as shown in Figure 1.1, as shown in Figure 1.2. Object calls to functions in the class:

Figure 1.2

Static member variables can be accessed by classes or objects

Access with class:
Maker::a=100;
Access with object:
Maker m;
m.a;

Static member functions can only access static member variables

Static member functions can only access static member variables, but ordinary member functions can access all variables, including static member variables, because static member functions do not pass in this pointer, which is explained below.

The static member variable decorated with const should be initialized in the class

The static member variable modified by const is best initialized within the class, although it can also be initialized outside the class

class Maker4
{
public:
    const static int a=20;
    const static int b;
};
//Class can also be initialized
const int Maker4::b = 30;

Static member functions also have permissions

Static member functions also have permissions. If they are private, they cannot be accessed outside the class, while static member variables can still be accessed outside the class even if they are private

class Maker3
{
private:
    static void func()
    {
        cout << "a3=" << a3 << endl;
    }
private:
    static int a3;
};

this pointer

definition

1. When the names of formal parameters and member variables are the same, use this to distinguish them

class Maker2
{
public:
    int id;
    static int a;
public:
    //1. When the names of formal parameters and member variables are the same, use this to distinguish them
    Maker2(int id)
    {
        this->id = id;
    }
}
int Maker2::a = 10;

2. Return the object itself

class Maker2
{
public:
    int id;
    static int a;
public:
    //2. Return the object itself
    Maker2 &getMaker2()
    {
        return *this;//Useful when operator overloading
    }

};
int Maker2::a = 10;

The this pointer points to the member space of the object

This pointer refers to the member space of the object, so this pointer cannot point to static member variables

As shown in Figure 1.3

The point of this pointer cannot be changed

The point of this pointer cannot be changed. The essence of this pointer is as follows:

Maker *const this;

Every ordinary member function passes in the this pointer

Each ordinary member function compiler passes in this pointer, which also explains how the function can distinguish which object it is when many objects share the same ordinary member function. (static member function does not pass in this pointer, so it can only access static member variables)

The pointer does not occupy object space

The pointer is not a part of the object and will not occupy the space of the object. Only ordinary member variables can occupy the space of the object (see the figure).

Constant function

definition

Add const after function ()

class Maker
{
public:
    //1. Add const after function ()
    void printMaker()const
    {......}
public:
    int id;
    int age;
};

Ordinary member variables cannot be modified in a constant function

class Maker
{
public:
    void printMaker()const
    {
        //id = 100;err//2. Ordinary member variables cannot be modified in a constant function
    }
public:
    int id;
    int age;
};

const in constant function modifies this

const modifies the space pointed to by this pointer, in which the variables cannot be changed

        This is not added const of this Pointer Maker *const this;
        This is plus const of this Pointer const Maker *const this;//This is a constant function

mutable modifier member variable

mutable modified member variables can be modified in constant functions

class Maker
{
public:
    Maker(int id, int age)
    {
        this->id = id;
        this->age = age;
        score = 100;
    }
    void printMaker()const
    {
        score=200;
    }
public:
    int id;
    int age;
    mutable int score;//4.mutable modified member variables can be modified in constant functions
};

Constant object

definition

Add const before the data type to make the object called constant object. After the constant object is defined, its value can no longer be modified.

const Maker m(1, 18);

be careful

  1. A constant object cannot change the value of a normal member variable
  2. Constant objects cannot call ordinary member functions because ordinary member functions may have statements to modify ordinary member variables
  3. Constant objects can only call constant functions
  4. Constant objects can only modify mutable decorated member variables

Topics: C++