3-C++中类大小影响因素

  • 对齐规则
  • 虚继承
  • 虚函数(多个合并)
  • 静态变量
  • 成员变量的大小
cpp 复制代码
#include <iostream>
#include <cstddef>

using namespace std;

// 空类 
class EmptyClass {
};

// char
class charClass {
    char c;	
};
// int
class intClass {
    int a;	
};


// char+int
class charintClass {
    char c;	
    int a;	
};

// int+double
class intdoubleClass {	
    int a;	
    double d;
};


// 只有静态成员和函数
class StaticMemberClass {
private:
    static int static_var;    // 不影响类大小
    int normal_var;           // 影响
public:
    static void static_func() {}  // 不影响
    void normal_func() {}         // 不影响
};
// 静态成员变量必须在类外定义
int StaticMemberClass::static_var = 0;

// 有虚函数的类
class VirtualFunctionClass {
private:
    char c;
    
public:
    virtual void virtual_func() {}    // 引入虚函数表指针
    void normal_func() {}
};

// 4. 单继承
class Base {
private:
    int base_data;
};

class Derived : public Base {
private:
    int derived_data;
};

// 5. 多继承
class Base1 {
private:
    int base1_data;
};

class Base2 {
private:
    int base2_data;
};

class MultipleDerived : public Base1, public Base2 {
private:
    int derived_data;
};

// 虚继承
class VirtualBase {
private:
    int virtual_base_data;
};

class VirtualDerived : virtual public VirtualBase {
private:
    //	vbtptr 虚基类表指针
    int virtual_derived_data;
    char c;
    // virtual_base_data
};

// 包含虚函数的多继承
class BaseWithVirtual {
private:
    int base_data;
public:
    virtual void base_virtual() {}
};

class AnotherBase {
private:
    int another_data;
};

class ComplexDerived : public BaseWithVirtual, public AnotherBase {
private:
    int complex_data;
public:
    virtual void derived_virtual() {}
};

// 测试内存对齐
class AlignmentTest {
private:
    char c;      // 1字节
    int i;       // 4字节
    double d;    // 8字节
    short s;     // 2字节
};

class OptimizedAlignment {
private:
    double d;    // 8字节
    int i;       // 4字节
    short s;     // 2字节
    char c1;      // 1字节
    char c2;      // 1字节
};

int main() {
     // 输出各平台相关信息
    cout << "=== 平台信息 ===" << endl;
    cout << "指针大小: " << sizeof(void*) << " 字节" << endl;
    cout << "int大小: " << sizeof(int) << " 字节" << endl;
    cout << "double大小: " << sizeof(double) << " 字节" << endl;
    	
    cout << "=== C++类大小影响因素分析 ===" << endl;
    cout << "sizeof(EmptyClass): " << sizeof(EmptyClass) << " 字节" << endl;
    cout << "sizeof(charClass): " << sizeof(charClass) << " 字节" << endl;
    cout << "sizeof(intClass): " << sizeof(intClass) << " 字节" << endl;
    cout << "sizeof(charintClass): " << sizeof(charintClass) << " 字节" << endl;
    cout << "sizeof(intdoubleClass): " << sizeof(intdoubleClass) << " 字节" << endl;
    cout << "sizeof(StaticMemberClass): " << sizeof(StaticMemberClass) << " 字节" << endl;
    cout << "sizeof(VirtualFunctionClass): " << sizeof(VirtualFunctionClass) << " 字节" << endl;
    cout << endl;
    
    cout << "=== 继承对类大小的影响 ===" << endl;
    cout << "sizeof(Base): " << sizeof(Base) << " 字节" << endl;
    cout << "sizeof(Derived): " << sizeof(Derived) << " 字节" << endl;
    cout << "sizeof(Base1): " << sizeof(Base1) << " 字节" << endl;
    cout << "sizeof(Base2): " << sizeof(Base2) << " 字节" << endl;
    cout << "sizeof(MultipleDerived): " << sizeof(MultipleDerived) << " 字节" << endl;
    cout << endl;
    
    cout << "=== 虚继承对类大小的影响 ===" << endl;
    cout << "sizeof(VirtualBase): " << sizeof(VirtualBase) << " 字节" << endl;
    cout << "sizeof(VirtualDerived): " << sizeof(VirtualDerived) << " 字节" << endl;
    cout << endl;
    
    cout << "=== 虚函数在多继承中的影响 ===" << endl;
    cout << "sizeof(BaseWithVirtual): " << sizeof(BaseWithVirtual) << " 字节" << endl;
    cout << "sizeof(AnotherBase): " << sizeof(AnotherBase) << " 字节" << endl;
    cout << "sizeof(ComplexDerived): " << sizeof(ComplexDerived) << " 字节" << endl;
    cout << endl;
    
    cout << "=== 内存对齐的影响 ===" << endl;
    cout << "sizeof(AlignmentTest): " << sizeof(AlignmentTest) << " 字节" << endl;
    cout << "sizeof(OptimizedAlignment): " << sizeof(OptimizedAlignment) << " 字节" << endl;
    cout << endl;
    
   
    
    return 0;
}
相关推荐
程序喵大人4 分钟前
【C++进阶】STL容器与迭代器 - 04 list 和 forward_list 用节点换稳定位置
开发语言·c++·list
qq_3660862214 分钟前
关于接口路径中中文值解码问题
java·开发语言·数据库
麻瓜老宋16 分钟前
AI开发C语言应用按步走,表达式计算器calc的第二十步,魔法数修复、测试分隔符、位运算截断检查、进制溢出检查
c语言·开发语言·atomcode
冻柠檬飞冰走茶34 分钟前
PTA基础编程题目集 7-11 分段计算居民水费(C语言实现)
c语言·开发语言·算法
An_s35 分钟前
Java Spring Boot+vue3文件管理系统
java·开发语言
名字还没想好☜44 分钟前
Go for-range 循环变量陷阱:goroutine 里全打印同一个值(Go 1.22 前后差异)
开发语言·后端·golang·go
倒流时光三十年1 小时前
第一阶段 05 · Java 客户端查询类详解(Query / SearchCriteria / Response 与复杂拼接)
java·开发语言·python
心平气和量大福大1 小时前
C#-WPF-控件-LiveChart图表-线性2(LineSeries)-数据绑定
开发语言·c#·wpf
lingran__1 小时前
C++_stack和queue和priority_queue(容器适配器)
开发语言·c++
小保CPP1 小时前
OpenCV C++基于极值区域滤波算法的场景文本检测(OCR)
c++·人工智能·opencv·算法·计算机视觉·ocr