【Geekband】专题二:虚指针和内存分配

1. 传统继承类的设计

static void print_object(const char* name, void* this_, size_t size) {
      void** ugly = reinterpret_cast(this_);
      size_t i;
      printf("created %s at address %p of size %zu\n", name, this_, size);
      for(i = 0 ; i < size / sizeof(void*) ; i++) {
        printf("  pointer[%zu] == %p\n", i, ugly[i]);
      }
      cout << "-------------------------------"<
  • A、B、C依次继承
  • 并在构造过程中通过print_object输出构建信息

2. 构造Class C,并通过指针来一次访问内容或调用函数

int main()
{
    typedef void(*Fun)(void);
    long** pVtab = nullptr;
    Fun pFun = nullptr; 
    C c; 
    pVtab = (long**)&c;
    long *cAsLong = (long *)&c;        
    cout << "C::vptr memory address is " << &cAsLong[0] <" << endl;
    cout << "    No.\tMemory Address\t Value\t\tFunction" << endl;
    for (int i=0; (Fun)pVtab[0][i] != nullptr; i++){
                pFun = (Fun)pVtab[0][i];
                cout << "    ["< 
  

你可能感兴趣的:(【Geekband】专题二:虚指针和内存分配)