Search code examples
c++intelicc

Where does Intel C++ Compiler store the vptr ( pointer to virtual function table ) in an Object?


Where does Intel C++ Compiler store the vptr ( pointer to virtual function table ) in an Object ?

I believe MSVC puts it at the beginning of the object, gcc at the end. What is it for icpc ( Intel C++ Compiler )?


Solution

  • For Intel C++ compiler, for Linux, I found it to be the beginning of the object.

    Code:

    #include <cstdio>
    
    class A 
    {
      int a, b;
    public:
      A(int a1, int b1): a(a1), b(b1) {}
      virtual void p(void) { printf("A\n"); }
    };
    
    class B: public A
    {
    public:
      B(int a1, int b1): A(a1, b1) {}
      void p(void){ printf("B\n"); }
    };
    
    int main(void)
    {
      A a(1, 2); int p=10; A a1(5, 6);
      B b(3, 4); int q=11; B b2(7, 8);
    
      a.p();
      b.p();
    
      int * x=(int*)&a;
      printf("%d %d %d %d\n", x[0], x[1], x[2], x[3]);
      x=(int*)&b;
      printf("%d %d %d %d\n", x[0], x[1], x[2], x[3]);
    }