C++ member function storage method | storage method

C++ member function storage method

In C++, when using classes to define objects, the system allocates storage space for each object. If a class includes data and functions, storage space should be allocated for data and function codes respectively.

The storage space occupied by each object of C++ is only the storage space occupied by the data part of the object, and does not include the storage space occupied by the function code.

The size of the space occupied by an object depends only on the space occupied by the data members of the object, and has nothing to do with member functions. The function code is stored outside the object space.

Although the same function code is executed when calling member functions of different objects, the execution results are generally different. Different objects use the same function code segment. C++ specifically sets up a pointer named this for this purpose. Used to point to different objects.

Regardless of whether the member function is defined in the class or outside the class, the code segment of the member function is stored in the same way.
Don’t confuse the storage method of member function with the concept of inline function.
The member function of a certain object is often said from a logical point of view, while the storage method of member function is from a physical point of view. The two are not contradictory.

Above, if you read and think it is helpful to you, please give Xiaolin a compliment, so that Xiaolin will also have the motivation to update, thank you fathers and villagers~

C++ member function storage method

More cases can go to the public account: C language entry to mastery

Guess you like

Origin blog.csdn.net/weixin_48669767/article/details/112910616