在c++; 我遇到过一些结构,这些结构似乎是用函数调用定义的,在一些C++代码中,我需要帮助来解释。例如,在中,有以下结构定义 /** * A binary identifier representing a resource. Internally it * is a 32bit integer split as follows: * * 0xPPTTEEEE * * PP: 8 bit package identifier. 0x01 is reserved for system * and 0x7f is reserved for the running app. * TT: 8 bit type identifier. 0x00 is invalid. * EEEE: 16 bit entry identifier. */ struct ResourceId { uint32_t id; ResourceId(); ResourceId(const ResourceId& rhs); ResourceId(uint32_t res_id); // NOLINT(google-explicit-constructor) ResourceId(uint8_t p, uint8_t t, uint16_t e); // Returns true if the ID is a valid ID that is not dynamic (package ID cannot be 0) bool is_valid_static() const; // Returns true if the ID is a valid ID or dynamic ID (package ID can be 0). bool is_valid() const; uint8_t package_id() const; uint8_t type_id() const; uint16_t entry_id() const; std::string to_string() const; };

在c++; 我遇到过一些结构,这些结构似乎是用函数调用定义的,在一些C++代码中,我需要帮助来解释。例如,在中,有以下结构定义 /** * A binary identifier representing a resource. Internally it * is a 32bit integer split as follows: * * 0xPPTTEEEE * * PP: 8 bit package identifier. 0x01 is reserved for system * and 0x7f is reserved for the running app. * TT: 8 bit type identifier. 0x00 is invalid. * EEEE: 16 bit entry identifier. */ struct ResourceId { uint32_t id; ResourceId(); ResourceId(const ResourceId& rhs); ResourceId(uint32_t res_id); // NOLINT(google-explicit-constructor) ResourceId(uint8_t p, uint8_t t, uint16_t e); // Returns true if the ID is a valid ID that is not dynamic (package ID cannot be 0) bool is_valid_static() const; // Returns true if the ID is a valid ID or dynamic ID (package ID can be 0). bool is_valid() const; uint8_t package_id() const; uint8_t type_id() const; uint16_t entry_id() const; std::string to_string() const; };,c++,struct,C++,Struct,我不明白这部分: ResourceId(); ResourceId(const ResourceId& rhs); ResourceId(uint32_t res_id); // NOLINT(google-explicit-constructor) ResourceId(uint8_t p, uint8_t t, uint16_t e); 这些是函数调用吗?它们如何成为结构定义的一部分?我找到了,但不确定它是否相关。这些行只是struct ResourceId的构造

我不明白这部分:

  ResourceId();
  ResourceId(const ResourceId& rhs);
  ResourceId(uint32_t res_id);  // NOLINT(google-explicit-constructor)
  ResourceId(uint8_t p, uint8_t t, uint16_t e);

这些是函数调用吗?它们如何成为结构定义的一部分?我找到了,但不确定它是否相关。

这些行只是
struct ResourceId
的构造函数:

ResourceId();
资源ID(常量资源ID和rhs);
资源id(uint32资源id);//NOLINT(谷歌显式构造函数)
资源ID(uint8\u t p、uint8\u t t、uint16\u t e);
实际上,
struct
s可以有构造函数:

另见

你有C++教材吗?这是在引言部分解释的吗?在C++教科书中对类和它们的方法的解释有什么不清楚的地方吗?提醒:结构与C++中的类相同,只是成员(函数或变量)的默认类别是公共的。我建议最好的编程实践是始终指定类(或结构)成员是公共的、受保护的还是私有的。如果这样做,实际上会使结构和类完全相同。struct确实提供了与C代码的原始向后兼容性。
struct
是一个
唯一的区别是
struct
具有默认的公共访问权限,而
class
具有默认的私有访问权限。您链接的问题完全无关-这是关于C的,它是一种非常不同的语言。哎呀,嗯。感谢所有有用的评论和答案。我曾考虑删除这篇文章,但据我所知,我不能,因为已经有一个答案了。无论如何,我更愿意投票,选择它作为被接受的答案,这样作者就可以得到分数。