Jianwei Sun <js***********@gmail.comkirjutas:
Hello saneman,
>Are protected fields not only relevant when using inheritance?
I am currently changing some code that use inheritance to code that
does not use inheritance and don't see the point in keeping the fields
protected.
Yes, and you may want to change it to private.
Even in case of inheritance, protected data members are often not a good
solution as it might make it impossible for the base class to ensure its
invariants.
A protected data member makes sense only when assigning *any* value to it
would not break the class invariants. Note that if this is the case one
could as well make the data member public instead of private when
redesigning the classes to not use inheritance. This would avoid writing a
pair of trivial set/get functions.
Paavo