Function member of C ++ Object Model (5)

zhaozj2021-02-17  57

1.1. Point to the function pointer of the member function

1.1.1. Static member function pointer

Double (Point :: * Coord) () = & point :: x;

Coord pointing to the actual address of X.

1.1.2. Non-virtual non-static member function pointer

Double (Point :: * Coord) () = & point :: x;

The pointer is obtained is the true address of X in memory, but this value is not complete, he needs to be bound to a Class Object address to call the function. The above pointer call:

a) (Origin. * Coord) ();

b) (PTR -> * coord) ();

Will be transformed into:

a) (& Origin);

b) (COORD) (PTR);

Such call costs, if you don't have Virtual, multiple inheritance or virtual base clars, is not high than non-member function pointers.

1.1.3. Virtual member function pointer

Double (Point :: * Coord) () = & point :: x;

The pointer is obtained is the index value of X in the Virtual Table.

(PTR -> * coord) ();

Will be converted to:

(* PTR-> VPTR [(int) Coord]) (PTR);

So how can you know that COORD makes the address or index? This must be introduced into a more general rule.

1.1.4. Member function pointer under multiple inheritance

0001 struct _mptr0002 {0003 INT DELTA; // THIS pointer's OFFSET value 0004 int index; // VTBL 0005 Union 0006 {0007 PTRTOFUNC FADDR; / / Function address 0008 INT V_OFFSET; // Virtual or multiple inheritance Two or subsequent Base Class VPTR

//, if the VPTR is placed in the beginning of the object, it is useless, the price is compatible.

/ Decrease 0009}; 0010};

(PTR -> * coord) ();

Will be converted to:

(Coord.index <0)

? // Non-virtual function

(* coord.faddr) (PTR)

: // virtual function

(* ptr-> vptr [coord.index] (PTR));

1.2. Function efficiency

effectiveness

name

high

Low Inline Menber Non-member Function (Nomember) Static member function (Nostatic Member) MEMBER Multi-inheritance Function (Virtual MEMBER) Virtual Inheritance Function (Virtual MEMBER) )

1.3. Function pointer efficiency

effectiveness

name

high

Pointer to non-member function Pointer Pointer Pointer Multiple Inheritance Non-virtual Function Pointer Multiple Inherited Non-virtual Function Pointer Pointer Pointer Pointer Pointer Pointer Multi - inherited Multi - inherited Virtual Members Function Pointer

转载请注明原文地址:https://www.9cbs.com/read-29278.html

New Post(0)