honestly, in my opinion, using protected is definitely more convenient.. |
C.133: Avoid protected data Reason protected data is a source of complexity and errors. protected data complicates the statement of invariants.... ... Note: Protected data often looks tempting to enable arbitrary improvements through derivation. Often, what you get is unprincipled changes and errors. Prefer private data with a well-specified and enforced invariant. Alternative, and often better, keep data out of any class used as an interface. Note Protected member function can be just fine. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rh-protected |
41. Make data members private, except in behaviorless aggregates ... Protected data has all the drawbacks of public data, because having protected data still means that an abstraction is sharing the responsibility of maintaining some invariant with an unbounded set of codeāin this case, with the unbounded set of current and future derived classes. Further, any code can read and modify protected data as easily as public data by deriving a new class and using that to get at the data. ... Nonprivate data members are almost always inferior to even simple passthrough get/set functions, which allow for robust versioning. |