博客
关于我
c++基础——类的大小
阅读量:490 次
发布时间:2019-03-06

本文共 1142 字,大约阅读时间需要 3 分钟。

C++类大小分析

在C++编程中,类的大小计算是一个常见但容易引起混淆的问题。本文将通过一个实例详细分析类的大小计算规则,并探讨如何影响类的大小。

实例代码

#include 
using std::cout;using std::endl;class Father {private: int age;public: void showAge() { cout << "Age: " << age << endl; }};class Mother {};class Son : public Father {};class Sister : public Father {private: static int a;};int main() { cout << "Size of int : " << sizeof(int) << endl; cout << "Size of Father: " << sizeof(Father) << endl; cout << "Size of Mother: " << sizeof(Mother) << endl; cout << "Size of Son : " << sizeof(Son) << endl; cout << "Size of Sister: " << sizeof(Sister) << endl; return 0;}

执行结果分析

  • 类的大小与方法无关

    • int的大小为4字节,Father类的大小也为4字节。
    • Mother类为空类,大小为1字节。
    • Son类继承自Father,大小仍为4字节。
    • Sister类同样继承自Father,但添加了一个静态变量a,静态变量不占用类大小。
  • 空类的大小

    • 一个空类的大小始终是1字节,而不是0。
  • 继承与私有变量

    • 子类SonSister包含父类Father的私有变量age,但子类的大小与父类相同。
    • 静态变量不占用类大小,Sister类的静态变量a不会影响类的大小。
  • 虚函数的影响

    Father类中的showAge()改为虚函数:

    class Father {private:	int age;public:	virtual void showAge() {		cout << "Age: " << age << endl;	}};

    此时,Father类的大小增加了4字节。这是因为虚函数引入了一个指向虚函数表的指针,大小为4字节。

    总结

    • 类的大小主要由其成员变量和非静态成员函数决定。
    • 空类的大小始终为1字节。
    • 虚函数的引入会增加类的大小,增加的字节数等于指针的大小(通常为4字节)。

    通过以上分析,可以清晰地理解C++类大小计算的规则及其影响。

    转载地址:http://puodz.baihongyu.com/

    你可能感兴趣的文章
    org.apache.axis2.AxisFault: org.apache.axis2.databinding.ADBException: Unexpected subelement profile
    查看>>
    sql查询中 查询字段数据类型 int 与 String 出现问题
    查看>>
    org.apache.commons.beanutils.BasicDynaBean cannot be cast to ...
    查看>>
    org.apache.dubbo.common.serialize.SerializationException: com.alibaba.fastjson2.JSONException: not s
    查看>>
    sqlserver学习笔记(三)—— 为数据库添加新的用户
    查看>>
    org.apache.http.conn.HttpHostConnectException: Connection to refused
    查看>>
    org.apache.ibatis.binding.BindingException: Invalid bound statement错误一例
    查看>>
    org.apache.ibatis.exceptions.PersistenceException:
    查看>>
    org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned
    查看>>
    org.apache.ibatis.type.TypeException: Could not resolve type alias 'xxxx'异常
    查看>>
    org.apache.poi.hssf.util.Region
    查看>>
    org.apache.xmlbeans.XmlOptions.setEntityExpansionLimit(I)Lorg/apache/xmlbeans/XmlOptions;
    查看>>
    org.apache.zookeeper.KeeperException$ConnectionLossException: KeeperErrorCode = ConnectionLoss for /
    查看>>
    org.hibernate.HibernateException: Unable to get the default Bean Validation factory
    查看>>
    org.hibernate.ObjectNotFoundException: No row with the given identifier exists:
    查看>>
    org.springframework.boot:spring boot maven plugin丢失---SpringCloud Alibaba_若依微服务框架改造_--工作笔记012
    查看>>
    SQL-CLR 类型映射 (LINQ to SQL)
    查看>>
    org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
    查看>>
    org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
    查看>>
    org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size exceeded
    查看>>