-2

I have come across structs that are defined seemingly with function calls inside, in some actual c++ code, that I need help to interpret. For example, in Resource.h from the AOSP, there is the following struct definition

/**
 * 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;
};

I don't understand this part:

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

Are these function calls? How can they be part of a struct definition? I found this SO post but am not sure if it is relevant/related.

auspicious99
  • 3,902
  • 1
  • 44
  • 58
  • 2
    Do you have a C++ textbook, where this is explained in the introductory chapter? Is there anything in your C++ textbook's explanations of classes and their methods that's unclear to you? – Sam Varshavchik May 04 '21 at 10:59
  • 3
    Reminder: a struct is the same as a class in C++ except that the default category for members (functions or variables) is public. I would suggest it's good programming practice to always specify whether class (or struct) members are public, protected or private. If you do this you in effect make a struct and a class identical. struct is really there to provide original backward compatibility with C code. – Roger Cigol May 04 '21 at 11:02
  • 1
    A `struct` is a `class` the only difference is `struct` has default public access and `class` has default private access – Richard Critten May 04 '21 at 11:02
  • 1
    The question you linked is completely irrelevant - it's about C, which is a very different language. – molbdnilo May 04 '21 at 11:20
  • Oops, duh. Thanks for all the helpful comments and the answer. I was considering deleting this post, but as I understand it, I can't as there is already an answer with upvotes. Anyway, I'd prefer to upvote and choose it as the accepted answer, so the author can get the points. – auspicious99 May 04 '21 at 11:27

1 Answers1

4

These lines are just constructors of 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);

Indeed, structs can have constructors: Struct Constructor in C++?

See also https://en.cppreference.com/w/cpp/language/constructor

A. Gille
  • 912
  • 6
  • 23