C ++ object model, const, new, and delete, and their heavy load descriptions for advanced programming, constdelete

Source: Internet
Author: User

C ++ object model, const, new, and delete, and their heavy load descriptions for advanced programming, constdelete
1. Object Model: Virtual pointer and virtual table: vptr and vtbl

1. Virtual pointers and virtual tables exist at the underlying layer;

2. A subclass object has a parent class. When a class has a virtual function, this object has a pointer, the size of objects with virtual functions is equal to the space occupied by all data plus 4. as shown in, B is a subclass of a. In addition to its own m_data3 data, B also includes Class a pointers and two data types. In addition to the data of a, the subclass inherits the virtual function call permission of the parent class. The parent class has virtual functions, and the subclass must have virtual functions. Class B inherits two virtual functions of Class a, in which vfunc1 is rewritten. In addition, its own function func2 is defined, and B has three functions in total. The same is true for c. Therefore, three Classes define a total of eight functions, four virtual functions and four non-virtual functions.

3. Assume that the pointer P is used to call vfunc1 () of Class c (). Dynamic binding is used for implementation. The virtual pointer vptr is used to find the corresponding function pointer in vtbl and find the corresponding function. Instead of using static binding (call XXX)

Implementation of a PowerPoint:

The Call action is compiled as a dynamic binding condition:

1. It must be called by pointer.

2. the pointer is an upward Transformation

3. virtual functions are called.

2. About this

This is also a pointer concept. In this example, this points to the myDoc address. It meets the dynamic binding conditions, so it triggers dynamic binding during the running process. When calling the vS (virtual Serialise () function, it points to the vS function of the subclass.

3. About const

1. the Const can only be placed behind the member function. Generally, the global function cannot add the const to that position.

2. If const is put there, it means to tell the compiler that I don't want to change the data in this function. If you do not add a const, the data may be changed, but the data cannot be changed.

3. When an object calls a function, the function may be const or not const.

4. about new and delete

Working Mechanism of new and delete expressions:

Example:

string* sp=new string("hello");string* arr=new string[10];

Step 1: The new expression calls a standard library function named operator new, which allocates a large enough, original, and untitled memory space;

Step 2: the compiler runs the corresponding constructor to construct these objects and assign the initial values;

Step 3: After the object is allocated space and constructed, a pointer to the object is returned.

delete sp;delete [] arr;

Step 1: Execute the corresponding destructor for the elements in the array indicated by the pointer;

Step 2: the compiler calls a standard function named operator delete to release the memory.

1. the application can define operator new and operator delete functions globally, or define them as member functions. When the compiler discovers a new or delete function expression, the operator function that can be called will be searched in the program. If the assigned object is of the tired type, the compiler first looks for it in the scope of the class and its base class. In this case, if the class contains the operator new member or the operator delete member, the corresponding expression will call these members. Otherwise, the compiler searches for matched functions in the global scope.

2. You can use the scope operator to make the new expression or delete expression ignore the function defined in the class and directly execute the version in the global scope. For example,: new only searches for the matching operator new function in the global scope, and: delete is similar to this function.

3. The standard library defines eight versions of the operator new and operator delete functions. The first four may throw the bad_alloc exception, but the last four may not.

Versions that may throw exceptions:

void *operator new(size_t); void *operator new[] (size_t); void *operator delete (void*) noexcept; void *operator delete[] (void*) noexcept;

No exception version is thrown:

void *operator new(size_t,nothrow_t&) noexcept;void *operator new[] (size_t,nothrow_t&) noexcept;void *operator delete (void*, nothrow_t&) noexcept;void *operator delete[] (void*,nothrow_t&) noexcept;

The application can customize any of the above function versions, but the custom version must be in the global scope or Class scope.

For operator new or operator new [], the return value type must be void *.

For operator delete or operator delete [], the return value type must be void.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.