结构后的公共是什么意思

What does : public after struct mean?

本文关键字:是什么 意思 结构      更新时间:2023-10-16
    struct hpet : public description_table_header
{
    uint8_t hardware_rev_id;
    uint8_t comparator_count:5;
    uint8_t counter_size:1;
    uint8_t reserved:1;
    uint8_t legacy_replacement:1;
    pci_vendor_t pci_vendor_id;
    address_structure address;
    uint8_t hpet_number;
    uint16_t minimum_tick;
    uint8_t page_protection;
} __attribute__((packed));

为什么结构体名称后面有公共__attribute__((packed)),为什么被打包在偏执狂中???这是 HPET(高精度事件计时器)的表。

代码是C++的,而不是你标记的 C。这是一个非常重要的细节。

此处public是继承的访问说明符(结构继承自 description_table_header )。这将在所有好的C++教科书中涵盖。关于SO的一个相关问题是:私有、公共和受保护继承之间的区别。实际上不需要structpublic规范,因为对于结构来说,这是默认值。

__attribute__((packed))是特定于编译器的扩展,用于指定struct的布局。在这种情况下,struct被打包,以便结构中没有填充。

在C++中,冒号:后跟可选的 public/private/protected 指示符,类型名称是指定继承的语法。

在代码中,hpet类继承description_table_header类。

有关公共、私有和受保护继承之间差异的讨论,请参阅此答案。