Rereading depth exploration of C ++ Object Model: function

Member function calls:

1. The non-static member functions:

Static function calls actually went through the following process:

1. rewriting function prototype, this placement of a pointer as an additional parameter

float Point3d::magnitude3d() const{};
float magituded3d(const Point3d*_this){};

2. The function of the non-static member accessed to be accessed via this pointer (static member does not belong to a specific example, it belongs to the category, not to be bound to this pointer)

3. The member function to re-write a new external function, a function name rewritten to make this unique function in the program

4.NRV optimization: the return value as a parameter, as a reference placed in the parameter list, return the value directly return.

X bar()--------->void bar(this,X&_result)

2. virtual member functions

If normalize () is a virtual member function, ptr-> normalize (); it is converted to:

(*ptr->vptr[1])(ptr)

 ptr via the vptr virtual function pointer, to find the corresponding virtual function table, then the corresponding function in which to find, jump to the function position, passing a pointer to this call.

 In the case of non-polymorphic, such as function calls obj.normalize () call does not go through the vptr, this is consistent with conventional non-static function efficiency.

3. static member function

Static member function can not directly access non-static member can not call non-static member functions. If you are calling non-static member function, you must pass an instance of the non-static member function belongs in the argument, to call non-static member function through the instance. This is because the non-static member function is not bound with this phase, it is necessary to specify a non-static member function call in the end belongs to which object.

Features static member function that:

1. can not directly access non-static members and non-static member function calls.

2. can not be declared as const, volatile or virtual. (Because it is not bound by this pointer) 

 

Virtual member functions:

For ptr-> z ()

What information is needed to make the right call z () entity at runtime:

It pointed to the real type 1.ptr

2.z () position

At compile time, the virtual address of the function to be finalized, and the addresses of these functions will not change the operation period.

Furthermore, placed in a new class in the vptr pointer, used to point to the virtual function table, virtual function is called by the virtual function table.

Under single inheritance: P157

This figure also too damn paste

 

Multiple inheritance follows:

Multiple virtual member functions in succession becomes complicated, mainly in the second and third or fourth base class ... who, because of the need to adjust the pointer in this implementation period

Assume:

class A{};

class B{};

class C:public A,public B{};

When using a base pointer:

A* a=new C;

At this point there is no difference between single inheritance, a point C of the starting address.

While in the second base class:

B* b=new C;

In this case b must be adjusted at the address, it points to the start address of the start of the B subobject, rather than objects.

Without this adjustment, the members of the class called through b B (not C), an error occurs: b-> data_B;

This is because the relationship between A and B is the same level, so the C, it is equivalent to inherit directly from B, C ++, and access to the members by actually starting address + offset to the class achieved, without considering the address a, B of the member is offset with respect to B, but now without adjustment point b, b-> data_B offset becomes the start address of a + B, and the access do not know what data members.

Meanwhile, if b is called a destructor: delete b;

At this time, but also to adjust the pointer to b to the beginning of the C.

This is because we want to ensure delete a delete b and achieve the same effect, i.e., Advanced C destructor, B is performed, performing the A destructor.

Without adjustment, the finished structure where analysis C, B will appear after the end ( is that right ??? ,)

The adjusted to the start address C, is guaranteed regardless of which base class pointer, are performed in the same order destructor.

Thus for a and b, even if they achieve the same result destructor, but in fact:

B destruction of the adjusted first pointer, which occurs in the run.

This is the first case, i.e., by pointing to a virtual function pointer to second and subsequent calls derived class.

The second situation is:

Virtual functions inherited from the second and subsequent calls the base class through a pointer pointing to a derived class.

C* c=new C;

c->funcb;

This is because c is the beginning point C to the starting address, the offset of each base class vptr is fixed, if the point c is not adjusted to the B subobject, then A will actually calls the inherited from the vptr virtual function tables point to the same position of the slot function, and the devil knows what this is.

So at this time to adjust to the point B object.

The third case is:

B* b1=new C;

B* b2=b1->func();

Explain func.

A function func, in both the virtual function B, C are also rewriting the virtual function, the function returns a A * in A, in B returns a B *, C in C returns a *.

First look b1-> func, b1 at this time is adjusted to c starting address, then the C version func is called, returns a pointer to C *, C * in this b2 obtained before position adjustment is necessary to return the pointer B subobejtct to place, if B b2 = new C in general.

Otherwise b2 will point to the beginning of the C's, which led to many problems call occurred.

P165 chart in the book that you map a little fuzzy TM

 

 Another point:

B* b=new C;

B only accessible via a base member B class, the derived class can not access the new members of class A is even. Because of static binding b b is a well-known class of B, so he can only access the members of the class B.

But we can dynmic_cast or other conversion mode to convert him into a category C, so A, B, C's members can be called up.

Guess you like

Origin www.cnblogs.com/lxy-xf/p/11390210.html