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;
}
相关推荐
三品吉他手会点灯21 小时前
C语言学习笔记 - 50.流程控制4 - 流程控制为什么非常非常重要
c语言·开发语言·笔记·学习
一只旭宝1 天前
【C++入门精讲22】常见设计模式
c++·设计模式
在放️1 天前
Python 爬虫 · 第三方代理接入与合规使用
开发语言·爬虫·python
KANGBboy1 天前
java知识五(继承)
java·开发语言
c++之路1 天前
Bazel C++ 构建系列文档(三):构建第一个 C++ 项目
开发语言·c++
AI人工智能+电脑小能手1 天前
【大白话说Java面试题 第117题】【并发篇】第17题:线程有几种状态,之间如何转换?
java·开发语言·面试
旖-旎1 天前
《LeetCode 695 岛屿的最大面积 FloodFill DFS 解法》
c++·算法·力扣·深度优先遍历·floodfill
森G1 天前
61、信号与槽机制在 TCP 编程中的应用---------网络编程
网络·c++·qt·网络协议·tcp/ip
syagain_zsx1 天前
STL 之 vector 讲练结合
c++·算法
聚名网1 天前
域名net,com,cn有区别吗?有哪些不同呢?
服务器·开发语言·php