Position of the virtual function table pointer

Source: Internet
Author: User

XX software company's written questions:

In the C + + standard, the virtual table pointers are not specified in the memory structure of the class, and the implementations of the different compilers may not be the same. Implement a code that determines whether the current compiler places a virtual table pointer at the front or the back of the class's memory structure.

The answer at the time was:

#include <iostream>
using namespace Std;

typedef unsigned (*fun) (void);

Class Base
{
Public
Base (void) {}
Virtual unsigned foo (void)
{
cout<< "Construct Base" <<endl;
return 1;
}

Public

Virtual ~base () {}
};

Class Derive:public Base
{
Public
Derive (void) {}
Virtual unsigned foo (void)
{
cout<< "construct derive" <<endl;
return 1;
}
};

BOOL Vtlinpre ()/* Determine if the virtual table pointer is in front of the class structure, yes, return true/
{
Derive Dr;
int** PVTL = (int**) &dr;
Fun pfun = NULL;
Pfun = (Fun) pvtl[0][0];
unsigned ret = 0;
ret = Pfun ();
return (ret = 1)? True:false;
}

Test
int main ()
{
BOOL TMP = FALSE;
TMP = Vtlinpre ();
if (TMP)
{
cout<< "The pointer of vitrual table is in head of class" <<endl;
}else
{
cout<< "The pointer of Vitrual" "in" Class "<<endl;
}
return 1;
}

General VC and BCB are placed vptr in the first four bytes of the class instance, GCC is placed at the end. In some cases, you need to consider the position of the table pointer, such as serialization.

The above solution is cumbersome and incorrect, because when the virtual function table pointer is placed at the end of the class instance, the virtual table pointer obtained in this way is already incorrect, let alone it to invoke the virtual function.

In fact, the position of the virtual table pointer can be learned only by comparing the first address of the class instance to the address of the first member variable of the class.

Class A
{
A (void) {}
virtual void Foo (void) {}
};

Int main ()
{
     A;
     char *p1 = reinterpret_cast<char*> (&a);
     Char *p2 = reinterpret_cast<char*> (&A.N);
     if (p1 = p2)
     {
         cout<< "VPtr is in the" Class instance! " <<endl;
    }else
     {
        cout<< "VPtr is in the" Class instance! " <<endl;
    }
     return 1;
} 

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.