Should you ever use protected member variables?

后端 未结 10 1731
星月不相逢
星月不相逢 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:12

    Generally, if something is not deliberately conceived as public, I make it private.

    If a situation arises where I need access to that private variable or method from a derived class, I change it from private to protected.

    This hardly ever happens - I'm really not a fan at all of inheritance, as it isn't a particularly good way to model most situations. At any rate, carry on, no worries.

    I'd say this is fine (and probably the best way to go about it) for the majority of developers.

    The simple fact of the matter is, if some other developer comes along a year later and decides they need access to your private member variable, they are simply going to edit the code, change it to protected, and carry on with their business.

    The only real exceptions to this are if you're in the business of shipping binary dll's in black-box form to third parties. This consists basically of Microsoft, those 'Custom DataGrid Control' vendors, and maybe a few other large apps that ship with extensibility libraries. Unless you're in that category, it's not worth expending the time/effort to worry about this kind of thing.

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

    Should you ever use protected member variables?

    Depends on how picky you are about hiding state.

    • If you don't want any leaking of internal state, then declaring all your member variables private is the way to go.
    • If you don't really care that subclasses can access internal state, then protected is good enough.

    If a developer comes along and subclasses your class they may mess it up because they don't understand it fully. With private members, other than the public interface, they can't see the implementation specific details of how things are being done which gives you the flexibility of changing it later.

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

    For detailed info on .Net access modifiers go here

    There are no real advantages or disadvantages to protected member variables, it's a question of what you need in your specific situation. In general it is accepted practice to declare member variables as private and enable outside access through properties. Also, some tools (e.g. some O/R mappers) expect object data to be represented by properties and do not recognize public or protected member variables. But if you know that you want your subclasses (and ONLY your subclasses) to access a certain variable there is no reason not to declare it as protected.

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

    The general feeling nowadays is that they cause undue coupling between derived classes and their bases.

    They have no particular advantage over protected methods/properties (once upon a time they might have a slight performance advantage), and they were also used more in an era when very deep inheritance was in fashion, which it isn't at the moment.

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

    In general, I would keep your protected member variables to the rare case where you have total control over the code that uses them as well. If you are creating a public API, I'd say never. Below, we'll refer to the member variable as a "property" of the object.

    Here's what your superclass cannot do after making a member variable protected rather than private-with-accessors:

    1. lazily create a value on the fly when the property is being read. If you add a protected getter method, you can lazily create the value and pass it back.

    2. know when the property been modified or deleted. This can introduce bugs when the superclass makes assumptions about the state of that variable. Making a protected setter method for the variable keeps that control.

    3. Set a breakpoint or add debug output when the variable is read or written to.

    4. Rename that member variable without searching through all the code that might use it.

    In general, I think it'd be the rare case that I'd recommend making a protected member variable. You are better off spending a few minutes exposing the property through getters/setters than hours later tracking down a bug in some other code that modified the protected variable. Not only that, but you are insured against adding future functionality (such as lazy loading) without breaking dependent code. It's harder to do it later than to do it now.

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

    Most of the time, it is dangerous to use protected because you break somewhat the encapsulation of your class, which could well be broken down by a poorly designed derived class.

    But I have one good example: Let's say you can some kind of generic container. It has an internal implementation, and internal accessors. But you need to offer at least 3 public access to its data: map, hash_map, vector-like. Then you have something like:

    template <typename T, typename TContainer>
    class Base
    {
       // etc.
       protected
       TContainer container ;
    }
    
    template <typename Key, typename T>
    class DerivedMap     : public Base<T, std::map<Key, T> >      { /* etc. */ }
    
    template <typename Key, typename T>
    class DerivedHashMap : public Base<T, std::hash_map<Key, T> > { /* etc. */ }
    
    template <typename T>
    class DerivedVector  : public Base<T, std::vector<T> >        { /* etc. */ }
    

    I used this kind of code less than a month ago (so the code is from memory). After some thinking, I believe that while the generic Base container should be an abstract class, even if it can live quite well, because using directly Base would be such a pain it should be forbidden.

    Summary Thus, you have protected data used by the derived class. Still, we must take int o account the fact the Base class should be abstract.

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