C ++ Object-Oriented Programming Object Model (objectmodel) for vptr and vtbl, objectmodelvptr

Source: Internet
Author: User

C ++ Object-Oriented Programming Object Model (objectmodel) for vptr and vtbl, objectmodelvptr

1. objectmodel: About vptr and vtbl

Note:

1) Once the C ++ compiler finds that there are virtual functions in the class, it will generate a virtual function table for the class, add a pointer to the virtual function table in each instance of this type (that is, from the memory perspective, there will be one more pointer in the object containing the virtual function ). On 32 machines, a pointer occupies 4 bytes of memory, so the sizeof value is 4 ,. On 64-bit machines, a pointer occupies 8 bytes of memory, so the sizeof value is 8.

2) the virtual function table contains pointers (addresses ). Virtual pointers are associated with virtual functions, which are irrelevant to general functions.

3) C calls a function through static binding. C ++ uses dynamic binding to call functions. If C ++ calls virtual functions in the C style, it should be:

(* (P-> vptr) [n]) (p); // or (* p-> vptr [n]) (p );
4) inherits the function call permission. If the parent class has a virtual function, the subclass must have a virtual function. Example:
class A{public:virtual void vfunc1();virtual void vfunc2();        void func1();        void func();private:int m_data1, m_data2;};class B:public A {public:virtual void vfunc1();        void func2();private:int m_data3;};class C:public B{public:virtual void vfunc1();        void func2();private:int m_data1, m_data4;};

5) The C ++ binding function has two practices: static call binding. One is dynamic binding and dynamic binding.Three conditionsYes: it is called through pointers; it is transformed up; virtual functions are called.

6) declare a container with a pointer pointing to the parent class (declared as a pointer pointing to the parent class, but the bound (new) is a subclass ). When a subclass modifies a virtual function, it can call its own versions in sequence by traversing the container object and calling the virtual function, for example. (If else ...).

This approach of virtual functions is "polymorphism", which is also point toA, but actually points to different things. The following example illustrates this behavior.(Note this pointer here. Simply put, an object is used to call a function.Object addressThis pointer is used.All member functions have the hidden parameter this):

7) about Dynamic Binding ):

(* (P-> vptr) [n]) (p); // or (* p-> vptr [n]) (p );

It means finding its virtual pointer through the pointer, then finding its virtual function table, getting the nth number, and using it as a function pointer for calling. Because it is called through p, p is this pointer. (This sentence also explains the above multi-state behavior)

2. Talk about const

Const member functions (constant member function)

When an object calls a member function, the object may be const or not const, and the member function may be const or not const, so there are two cases.

Rule: When the const and non-const versions of the member functions exist at the same time, the const object will only (only) Call the const version, and the non-const object will only (only) call the non-const version. (If only one object exists, the const object can only call the const version. However, the non-const object may call the const or non-const version .)

The following example shows a poor design:

Const String str ("hello world"); str. print (); // If the const is not specified when Srting: print () is designed, the const object calls the non-const member function and an error occurs.

3. about new and deete

0) in C ++, size_t is designed to adapt to multiple platforms. The introduction of size_t enhances the portability of programs on different platforms.

1) new and delete are expressions that can be divided into (compiler conversion ).

2) overload: operator new,: operatordelete,: operator new [],: operator delete []

This is to overload the global function. Global is used without heavy load. Note that the delete operation must accept pointers.

3) Reload member operator new/delete

What should I do if I take over the memory allocation? Create a memory pool.

4) Reload member operator new []/delete []

Take over user behavior

Example, interface ......

If you want to bypass the designer's design, you can add a global scope. (Write global scope operator: will bypass all the aforementioned overloaded functions and force the use of global version)

5) Reload new (), delete ()

Placement new:

We can reload class member operatornew () to write multiple versions, provided that each version declaration must have a unique parameter column. The first parameter must be size_t, the remaining parameters take the placemnet arguments specified by new as the initial values. Appears in new (......) The so-called placement arguments is in parentheses.

We can also reload class member operatordelete () (or placement operator delete) to write multiple versions. But they will never be called by delete. Operatordelete () of these overloaded versions is called only when the ctor called by new throws an exception (). It can only be called in this way, mainly used to return the memory occupied by objects that fail to be fully created.

There is only one time for these corresponding delete calls, that is, after the corresponding new memory is allocated, if the constructor plays an exception (indicating that the object fails to be constructed ), then release the allocated memory.

6) basic_string use new (extra) to expand the application volume

(This part is not quite understandable. I will add it later .)

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.