[GeekBand] C ++ Advanced Programming Technology (2), geekband Advanced Programming

Source: Internet
Author: User

[GeekBand] C ++ Advanced Programming Technology (2), geekband Advanced Programming

This document is divided into two main parts: The first part is about the object model, and the second part is about the more in-depth study of new and delete.

2. Static binding and dynamic binding

In C, different function names are statically bound. Each function corresponds to an address and is stored in the corresponding location. In C ++, non-virtual member functions are also stored in static binding mode. For example, A: func1 () and other member functions.

However, for virtual functions, C ++ uses the dynamic binding method. In, each virtual function is stored in the virtual function table. When calling a virtual function, the compiler will find the correct function call along with the path in.

Because of dynamic binding, virtual functions can always be called wherever possible. This mechanism limits the possibility that virtual functions should be overwritten by virtual functions.

The following two methods can be used to obtain a virtual function.

A very common method based on virtual functions is to create a pointer linked list pointing to an abstract class, which is a manifestation of polymorphism.

The memory allocation of the class object model involves the bitwise alignment rules of objects, which is briefly introduced in another article in the blog.

  • Locate the new and delete operators and their Overloading

    When using new and delete, you can have an optional pointer type parameter to specify the starting address of memory allocation. Without this parameter, a suitable size of space is automatically allocated in the heap space.

The default new function is:

Void* Operator New (Size_t size,Void*Start)

You can use the following method for use: int * p = new (0x12345678) int;

In fact, we can also use other parameter columns for the new operation, or we can reload operator new and operator delete. For example, the following two common parameter columns:

Void* Operator New (Size_t size,Long extra)// The extra parameter is used to apply for a bucket. It is used to store special information, such as reference counting information.

Void* Operator New (Size_t size,Long extra,Char init)

Its inherent definition is:

Inline void* Operator New (Size_t size) {ReturnMalloc(Size);}

Inline void* Operator New [] (Size_t size) {ReturnMalloc(Size);}// The size is automatically calculated during the call.

Inline voidOperator Delete (Void*Ptr,Size_t size){Free(Ptr);}

Inline voidOperator Delete [] (Void*Ptr,Size_t size){Free(Ptr);}

Note: If you need to reload, the first parameter must always be in size_t format. In addition, the delete defined by myself will not be called, but will continue to use the default method to directly release the memory. Only when the new operation fails will the operator delete function with the same input parameter columns be searched. If the corresponding delete function is not defined, it can also be compiled, it means to discard the exception that failed to process new.

Related Article

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.