Should you ever use protected member variables?

后端 未结 10 1732
星月不相逢
星月不相逢 2020-12-02 07:53

Should you ever use protected member variables? What are the the advantages and what issues can this cause?

相关标签:
10条回答
  • 2020-12-02 08:28

    The key issue for me is that once you make a variable protected, you then cannot allow any method in your class to rely on its value being within a range, because a subclass can always place it out of range.

    For example, if I have a class that defines width and height of a renderable object, and I make those variables protected, I then can make no assumptions over (for example), aspect ratio.

    Critically, I can never make those assumptions at any point from the moment that code's released as a library, since even if I update my setters to maintain aspect ratio, I have no guarantee that the variables are being set via the setters or accessed via the getters in existing code.

    Nor can any subclass of my class choose to make that guarantee, as they can't enforce the variables values either, even if that's the entire point of their subclass.

    As an example:

    • I have a rectangle class with width and height being stored as protected variables.
    • An obvious sub-class (within my context) is a "DisplayedRectangle" class, where the only difference is that I restrict the widths and heights to valid values for a graphical display.
    • But that's impossible now, since my DisplayedRectangle class cannot truly constrain those values, as any subclass of it could override the values directly, while still being treated as a DisplayedRectangle.

    By constraining the variables to be private, I can then enforce the behaviour I want through setters or getters.

    0 讨论(0)
  • 2020-12-02 08:28

    Just for the record, under Item 24 of "Exceptional C++", in one of the footnotes, Sutter goes "you would never write a class that has a public or protected member variable. right? (Regardless of the poor example set by some libraries.)"

    0 讨论(0)
  • 2020-12-02 08:30

    At the design level it might be appropriate to use a protected property, but for implementation I see no advantage in mapping this to a protected member variable rather than accessor/mutator methods.

    Protected member variables have significant disadvantages because they effectively allow client code (the sub-class) access to the internal state of the base class class. This prevents the base class from effectively maintaining its invariants.

    For the same reason, protected member variables also make writing safe multi-threaded code significantly more difficult unless guaranteed constant or confined to a single thread.

    Accessor/mutator methods offer considerably more API stability and implementation flexibility under maintenance.

    Also, if you're an OO purist, objects collaborate/communicate by sending messages, not reading/setting state.

    In return they offer very few advantages. I wouldn't necessarily remove them from somebody else's code, but I don't use them myself.

    0 讨论(0)
  • 2020-12-02 08:38

    In short, yes.

    Protected member variables allow access to the variable from any sub-classes as well as any classes in the same package. This can be highly useful, especially for read-only data. I don't believe that they are ever necessary however, because any use of a protected member variable can be replicated using a private member variable and a couple of getters and setters.

    0 讨论(0)
提交回复
热议问题