zoukankan      html  css  js  c++  java
  • c++, class的大小

    不为类、对象的函数分配空间;

    在类中如果有virtual声明的虚函数,则会隐藏一个指针,该指针指向虚函数表,这对于纯虚函数也是一样;

    对于虚继承,还有一个指向父类的指针,该指针为指向虚基类的指针(Pointer to virtual base class)。

    #include "stdafx.h"
    #include <iostream>
    using namespace std;
    
    #include "t1.h"
    
    class TNULL
    {
    };
    class Ti
    {
        int i;
    };
    class Tic
    {
        int i;
        char c;
    };
    class Tf
    {
        void f(void){}
    };
    class T_vf
    {
        virtual void f(void){}
    };
    class Tif  
    {
    public:
        void set(int x)    {i = x;}
    private:
        int i;
    };
    class Ti_vf  
    {
    public:
        virtual void set(int x)    {i = x;}
    private:
        int i;
    };
    class Ti_v0f  
    {
    public:
        virtual void set(int x) = 0;
    private:
        int i;
    };
    
    class Ti_sf  
    {
        int i;
        static void sf(void){}
    };
    class TT
    {
        TT(){}
    };
    class TxT
    {
        ~TxT(){}
    };
    
    class TTT
    {
        TTT(){}
        ~TTT(){}
    private:
        Tif* m_pTif;
    };
    
    class TTTFFF
    {
    public:
        TTTFFF()    {
            m_pTif = new Tif;
            m_pTif->set(0);
        }
    
        ~TTTFFF(){
            delete(m_pTif);
            m_pTif = 0;
        }
        void f1(void){}
        void f2(void){}
        void f3(void){}
    private:
        Tif* m_pTif;
    };
    class TTT_VFFF
    {
    public:
        TTT_VFFF()    {
            m_pTif = new Tif;
            m_pTif->set(0);
        }
    
        ~TTT_VFFF(){
            delete(m_pTif);
            m_pTif = 0;
        }
        virtual void f1(void){}
        virtual void f2(void){}
        virtual void f3(void){}
    private:
        Tif* m_pTif;
    };
    void t1(void)
    { 
        cout<<"sizeof(char) = "<<sizeof(char)<<endl;
        cout<<"sizeof(class TNULL) = "<<sizeof(class TNULL)<<endl;
        cout<<"sizeof(class Ti) = "<<sizeof(class Ti)<<endl;
        cout<<"sizeof(class Tic) = "<<sizeof(class Tic)<<endl;
    
        cout<<"sizeof(class Tf) = "<<sizeof(class Tf)<<endl;
        cout<<"sizeof(class Tif) = "<<sizeof(class Tif)<<endl;
        cout<<"sizeof(class T_vf) = "<<sizeof(class T_vf)<<endl;
        cout<<"sizeof(class Ti_vf) = "<<sizeof(class Ti_vf)<<endl;
        cout<<"sizeof(class Ti_v0f) = "<<sizeof(class Ti_v0f)<<endl;
        
        cout<<"sizeof(class Ti_sf) = "<<sizeof(class Ti_sf)<<endl;
        cout<<"sizeof(class TT) = "<<sizeof(class TT)<<endl;
        cout<<"sizeof(class TxT) = "<<sizeof(class TxT)<<endl;
        cout<<"sizeof(class TTT) = "<<sizeof(class TTT)<<endl;
        cout<<"sizeof(class TTTFFF) = "<<sizeof(class TTTFFF)<<endl;
        cout<<"sizeof(class TTT_VFFF) = "<<sizeof(class TTT_VFFF)<<endl;
        
        TTTFFF t;
        cout<<"sizeof(TTTFFF t) = "<<sizeof(t)<<endl; /*sizeof(TTTFFF t) = 4*/
    
    
    }
    /*
    sizeof(char) = 1
    sizeof(class TNULL) = 1
    sizeof(class Ti) = 4
    sizeof(class Tic) = 8
    sizeof(class Tf) = 1
    sizeof(class Tif) = 4
    sizeof(class T_vf) = 4
    sizeof(class Ti_vf) = 8
    sizeof(class Ti_v0f) = 8
    sizeof(class Ti_sf) = 4
    sizeof(class TT) = 1
    sizeof(class TxT) = 1
    sizeof(class TTT) = 4
    sizeof(class TTTFFF) = 4
    sizeof(class TTT_VFFF) = 8
    sizeof(TTTFFF t) = 4
    请按任意键继续. . .
    */

    扩展:

    1.  C++类的大小 http://www.cnblogs.com/luxiaoxun/archive/2012/09/01/2666395.html

    2.  C++ 虚函数表解析 http://blog.csdn.net/haoel/article/details/1948051

  • 相关阅读:
    MHA
    主从复制
    模拟数据误删除并恢复
    mysql日志类型和备份mysqldump2
    springcloud-Hystrix图形化Dashboard搭建
    springcloud-Hystrix工作流程
    springcloud-Hystrix-服务熔断使用
    springcloud-Hystrix-服务熔断理论
    springcloud-Hystrix-降级fallback完善
    springcloud-Hystrix-降级fallback
  • 原文地址:https://www.cnblogs.com/mylinux/p/4725637.html
Copyright © 2011-2022 走看看