2

Structs interface and Hiding members in a C struct discusses various ways in accessing /modifying members.

What would be pros/cons of using:

  1. Opaque handle to struct and setters/getters
  2. Accessing members directly
  3. foo.value(&foo, value) functions (like C++ class methods)
  4. Separate header files for same struct exposing public members for client and all members internally

In my case, I'm doing OOP in C and all my structs hold a list of properties (id, name, desc, ...). I need to track changes so that changed status could be transmitted over network. The best way, as I see it, would be to transmit the delta (changes between individual members) and not retransmitting the whole struct.

Thank you

Community
  • 1
  • 1
mindless
  • 135
  • 2
  • 6
  • It is to list pros/cons of an each way so one could choose which suits him/her best instead of selecting blindly not considering possible future limitations. – mindless Aug 29 '11 at 08:33
  • SO is generally intended for asking questions. You can use other host, e.g. *The Code Project* for posting coding-related articles. – Petr Abdulin Aug 29 '11 at 09:31
  • 1
    It **IS A QUESTION** to list and discuss [pros and cons](http://wiki.answers.com/Q/What_are_pros_and_cons) of different ways accessing C struct members. – mindless Aug 29 '11 at 09:54
  • E.G. setters and getters leads to bloat but also provides encapsulation and way to track changes to particular members which would be impossible if we could access those members directly. Using an opaque pointer (compiler firewall) does not allow client to gain any insight of an internal layout of a struct but also makes harder to learn about possible interaction with a struct while looking at its header. – mindless Aug 29 '11 at 10:01
  • Well, then it's not a question, but duscussion. Even if you reformulate it as a question, it's not a best place to ask here: http://stackoverflow.com/faq#dontask . Consider posting same question at Programmers: http://programmers.stackexchange.com – Petr Abdulin Aug 29 '11 at 11:02
  • However, if your motivation is “I would like others to explain ______ to me”, then you are probably OK. I would like others to explain pros/cons of different ways accessing C struct members. – mindless Aug 29 '11 at 11:32
  • Any way, vote for closing this discussion if you feel like it. – mindless Aug 29 '11 at 11:35

0 Answers0