C++类的大小

类大小

1.空类占用一个字节。若父类为空类,创建子类对象时,父类不占用子类空间。

#include<iostream>
using namespace std;
class Father{
    public:
    Father(){}
    ~Father(){}
};
class Son:public Father{
    public:
    Son(){}
    ~Son(){}
};
int main()
{
    Father f;
    Son s;
    cout<<"sizeof(f):"<<sizeof(f)<<endl;
    cout<<"sizeof(s):"<<sizeof(s)<<endl;
    return 0;
}

运行结果:
在这里插入图片描述

#include<iostream>
using namespace std;
class Father{
    public:
    Father(){}
    ~Father(){}
};
class Son:public Father{
    public:
    Son(){}
    ~Son(){}
    int a;
};
int main()
{
    Father f;
    Son s;
    cout<<"sizeof(f):"<<sizeof(f)<<endl;
    cout<<"sizeof(s):"<<sizeof(s)<<endl;
    return 0;
}

运行结果:
在这里插入图片描述

2.构造函数和析构函数不占用类空间

#include<iostream>
using namespace std;
class Father{
    
};
class Son:public Father{
    public:
    Son(){}
    ~Son(){}
};
int main()
{
    Father f;
    Son s;
    cout<<"sizeof(f):"<<sizeof(f)<<endl;
    cout<<"sizeof(s):"<<sizeof(s)<<endl;
    return 0;
}

执行结果:
在这里插入图片描述

3.普通成员函数不占用类空间:

#include<iostream>
using namespace std;
class Father{
    public:
    int a;
    char b;
    // void test(){int a=5;}
};
class Son:public Father{
    public:
    Son(){}
    ~Son(){}
};
int main()
{
    Father f;
    Son s;
    cout<<"sizeof(f):"<<sizeof(f)<<endl;
    cout<<"sizeof(s):"<<sizeof(s)<<endl;
    return 0;
}

执行结果:
在这里插入图片描述

#include<iostream>
using namespace std;
class Father{
    public:
    int a;
    char b;
     void test(){int a=5;}
};
class Son:public Father{
    public:
    Son(){}
    ~Son(){}
};
int main()
{
    Father f;
    Son s;
    cout<<"sizeof(f):"<<sizeof(f)<<endl;
    cout<<"sizeof(s):"<<sizeof(s)<<endl;
    return 0;
}

在这里插入图片描述

4.一个类中只要有虚成员函数,且无论有多少个虚成员函数,都会开辟一块内存来存放虚函数表指针,这个指针的大小等于计算机的位数/1字节位数。

#include<iostream>
using namespace std;
class Father{
    public:
    int a;
    char b;
    virtual void test(){int a=5;}
};
class Son:public Father{
    public:
    Son(){}
    ~Son(){}
};
int main()
{
    Father f;
    Son s;
    cout<<"sizeof(f):"<<sizeof(f)<<endl;
    cout<<"sizeof(s):"<<sizeof(s)<<endl;
    return 0;
}

在这里插入图片描述

#include<iostream>
using namespace std;
class Father{
    public:
    int a;
    char b;
    virtual void test(){int a=5;}
    virtual void test01(){int b=6;}
};
class Son:public Father{
    public:
    Son(){}
    ~Son(){}
};
int main()
{
    Father f;
    Son s;
    cout<<"sizeof(f):"<<sizeof(f)<<endl;
    cout<<"sizeof(s):"<<sizeof(s)<<endl;
    return 0;
}

在这里插入图片描述

5.子类和父类共用虚函数表指针。

#include<iostream>
using namespace std;
class Father{
    public:
    int a;
    char b;
    virtual void test(){int a=5;}
    virtual void test01(){int b=6;}
};
class Son:public Father{
    public:
    Son(){}
    ~Son(){}
    void test(){int a=6;}
    void test01(){int b=7;}
    virtual void test02(){int c=5;}
};
int main()
{
    Father f;
    Son s;
    cout<<"sizeof(f):"<<sizeof(f)<<endl;
    cout<<"sizeof(s):"<<sizeof(s)<<endl;
    return 0;
}

在这里插入图片描述

6.即使父类是空类,只要将析构函数声明为虚函数,就会产生一个虚函数表指针。

#include<iostream>
using namespace std;
class Father{
    public:
    Father(){}
    virtual ~Father(){}
};
class Son:public Father{
    public:
    Son(){}
    ~Son(){}
};
int main()
{
    Father f;
    Son s;
    cout<<"sizeof(f):"<<sizeof(f)<<endl;
    cout<<"sizeof(s):"<<sizeof(s)<<endl;
    return 0;
}

在这里插入图片描述

7.类也会遵循结构体中字节对齐的规则。

#include<iostream>
using namespace std;
class Father{
    public:
    int a;
    char b;
     void test(){int a=5;}
};
class Son:public Father{
    public:
    Son(){}
    ~Son(){}
    char c;//
};
int main()
{
    Father f;
    Son s;
    cout<<"sizeof(f):"<<sizeof(f)<<endl;
    cout<<"sizeof(s):"<<sizeof(s)<<endl;
    return 0;
}

在这里插入图片描述

#include<iostream>
using namespace std;
class Father{
    public:
    int a;
    char b;
     void test(){int a=5;}
};
class Son:public Father{
    public:
    Son(){}
    ~Son(){}
    int c;//
};
int main()
{
    Father f;
    Son s;
    cout<<"sizeof(f):"<<sizeof(f)<<endl;
    cout<<"sizeof(s):"<<sizeof(s)<<endl;
    return 0;
}

在这里插入图片描述

8.静态成员不占用类的空间大小。

#include<iostream>
using namespace std;
class Father{
    
    public:
    int a;
    char b;
     void test(){int a=5;}
     static int g;
     
};
class Son:public Father{
    public:
    Son(){}
    ~Son(){}
    int c;
};
int main()
{
    Father f;
    Son s;
    cout<<"sizeof(f):"<<sizeof(f)<<endl;
    cout<<"sizeof(s):"<<sizeof(s)<<endl;
    return 0;
}

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值