"Reprint" C + + notes: In-depth understanding of data member pointers, function member pointers

Source: Internet
Author: User

The original:C + + notes: In-depth understanding of data member pointers, function member pointers

1. Data member pointers

For a normal pointer variable, the value is the address it points to, and 0 represents a null pointer.
For a data member pointer variable, the value is the offset value of the address of the data member relative to the start address of the object, and the null pointer is represented by-1. Cases:

code example:

View Code

2. function member pointers

The function member pointer, compared to the normal function pointer, has a size of twice times the normal function pointer (16 bytes under x64) and is divided into two parts: ptr and adj.

(1) Non-virtual function member pointers

The PTR part is a function pointer (pointing to a global function, the first parameter of the function is the this pointer), and the Adj section is always 0. Cases:

code example:

View Code

(2) virtual function member pointer

The PTR portion of the function pointer for the virtual function corresponds to the offset address in the virtual function table plus 1 (the reason for adding 1 is to use 0 to represent a null pointer), and the adj part is the offset byte number that adjusts the this pointer. Cases:

Description

    • Both A and B have no base classes, but there are virtual functions, so each has a virtual function pointer (assumed to be vptr).
    • C Inherits A and b at the same time, so it inherits two virtual function pointers, but in order to save space, C will be common with the main base class A virtual function pointer (that is, VPTR1), the virtual function pointer inheriting from B is assumed to be vptr2.
    • C does not override virtual functions that inherit from A and B, so there are a::foo and B::bar function pointers in the virtual function table of C (if Foo () is overridden in C, A::foo in the virtual function table of C is replaced with C::foo).
    • There are two virtual function pointers vptr1 and VPTR2 in C, which are equivalent to two virtual function tables.
    • A::foo (C::foo), B::bar (C::bar) all offset the position of address 0 in the virtual function table, so PTR is 1 (0+1=1). The C::quz is positioned at offset 8, so PTR is 9 (8+1=9).
    • When we use the PC to call C::bar (), such as: "(Pc->*pcbar) ()", which is actually called b::bar () (i.e. _zn1b3barev (PC)), the PC needs to be converted to the b* type pointer, Therefore, the this pointer needs to be adjusted (to the address that PB points to), so the Adj is 8.

code example:

extern "C" int printf (const char*, ...); struct A {virtual void foo () {printf ("A::foo (): this = 0x%p\n", this);}}; struct B {virtual void bar () {printf ("B::bar (): this = 0x%p\n", this);}}; struct C:public A, public B {virtual void Quz () {printf (' C::quz (): this = 0x%p\n ', this);}};   void (A::* pafoo) () = &A::foo;   Ptr:1, Adj:0void (B::* pbbar) () = &B::bar;   Ptr:1, Adj:0void (C::* pcfoo) () = &C::foo;   Ptr:1, Adj:0void (C::* pcquz) () = &C::quz;   Ptr:9, Adj:0void (C::* pcbar) () = &C::bar; Ptr:1, Adj:8#define part1_of_ptr (P) (((long*) &p) [0]) #define PART2_OF_PTR (P) (((long*) &p) [1]) int main (   ) {printf ("&a::foo->ptr:0x%lx,", Part1_of_ptr (Pafoo));   1 printf ("&a::foo->adj:0x%lx\n", Part2_of_ptr (Pafoo));   0 printf ("&b::bar->ptr:0x%lx,", Part1_of_ptr (Pbbar));   1 printf ("&b::bar->adj:0x%lx\n", Part2_of_ptr (Pbbar)); 0 printf ("&c::foo->ptr:0x%lx,", PARt1_of_ptr (Pcfoo));   1 printf ("&c::foo->adj:0x%lx\n", Part2_of_ptr (Pcfoo));   0 printf ("&c::quz->ptr:0x%lx,", Part1_of_ptr (Pcquz));   9 printf ("&c::quz->adj:0x%lx\n", Part2_of_ptr (Pcquz));   0 printf ("&c::bar->ptr:0x%lx,", Part1_of_ptr (Pcbar));   1 printf ("&c::bar->adj:0x%lx\n", Part2_of_ptr (Pcbar)); 8 return 0;}

Reference:

C + + ABI for itanium:2.3 Member pointers

"Reprint" C + + notes: In-depth understanding of data member pointers, function member pointers

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.