zoukankan      html  css  js  c++  java
  • class layout basic 2

    #include<iostream>
    using namespace std;
    
    //虚函数和析构函数需要定义,否则
    //a missing vtable usually means the first 
    //non-inline virtual member function has no definition.
    
    class a{
        virtual void func(){};
    public:
        virtual ~a()=default;//析构函数需要为public
    }aObj;
    
    class b :public virtual a{
        virtual void foo(){};
    public:
        virtual ~b()=default;
    }bObj;
    
    class ax{
        virtual void func(){};
        char x;
    public:
        virtual ~ax()=default;
    }axObj;
    
    class bx:public virtual ax{
        virtual void foo(){};
    public:
        virtual ~bx()=default;
    }bxObj;
    
    class c{
        virtual void func(){};
    public:
        virtual ~c()=default;
    }cObj;
    
    class d :public c{
        virtual void foo(){};
    public:
        virtual ~d()=default;
    }dObj;
    
    class cx{
        virtual void func(){};
    public:
        virtual ~cx()=default;
        char x;
    }cxObj;
    
    class dx:public cx{
        virtual void foo(){};
    public:
        virtual ~dx()=default;
    }dxObj;
    
    int main(){
        cout<<"aObj "<<sizeof(aObj)<<endl;
        cout<<"bObj "<<sizeof(bObj)<<endl;
        cout<<"axObj "<<sizeof(axObj)<<endl;
        cout<<"bxObj "<<sizeof(axObj)<<endl;
        cout<<"cObj "<<sizeof(cObj)<<endl;
        cout<<"dObj "<<sizeof(dObj)<<endl;
        cout<<"cxObj "<<sizeof(cxObj)<<endl;
        cout<<"dxObj "<<sizeof(dxObj)<<endl;
        return 0;
    }
    
    //clang3.8 gcc7.1
    //aObj    8
    //bObj    8
    //axObj   16
    //bxObj   16
    //cObj    8
    //dObj    8
    //cxObj   16
    //dxObj   16
  • 相关阅读:
    解决博客园中代码着色问题
    使用OutputDebugString输出调试信息
    Android编程小实验
    在安卓3.0以下版本使用Fragment的注意事项
    开源中国客户端源码阅读笔记
    C++基础
    IO流
    OAuth笔记
    反射
    链表
  • 原文地址:https://www.cnblogs.com/Searchor/p/9526566.html
Copyright © 2011-2022 走看看