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;
}
相关推荐
艾莉丝努力练剑13 小时前
【C++:C++11】C++11新特性深度解析:从可变参数模板到Lambda表达式
c++·stl·c++11·lambda·可变模版参数
同学小张15 小时前
【端侧AI 与 C++】1. llama.cpp源码编译与本地运行
开发语言·c++·aigc·llama·agi·ai-native
踢球的打工仔16 小时前
PHP面向对象(7)
android·开发语言·php
汤姆yu18 小时前
基于python的外卖配送及数据分析系统
开发语言·python·外卖分析
Yue丶越18 小时前
【C语言】字符函数和字符串函数
c语言·开发语言·算法
翔云 OCR API18 小时前
人脸识别API开发者对接代码示例
开发语言·人工智能·python·计算机视觉·ocr
V***u45319 小时前
MS SQL Server partition by 函数实战二 编排考场人员
java·服务器·开发语言
这是程序猿19 小时前
基于java的ssm框架旅游在线平台
java·开发语言·spring boot·spring·旅游·旅游在线平台
芳草萋萋鹦鹉洲哦19 小时前
【elemen/js】阻塞UI线程导致的开关卡顿如何优化
开发语言·javascript·ui
爱学习的小邓同学19 小时前
C++ --- 多态
开发语言·c++