3.7. Static Members of an Abstract Base Class

What about the text file against which the user queries are made? There is only one instance of the text file (or if we support multiple text files, one instance of each of the several text files). The text file, then, is represented as a static member, together with several supporting static methods and an indexer:

abstract public class Query
{
   static private [] string ms_textfiles;
   const  private    int    ms_maxFiles = 24;

   static protected void check_index( int ix )   {...}
   static public    void add_file( string name ) {...}

   static public int MaxFiles {
          get{ return ms_MaxFiles; }
   }

   static public string this[ index ix ]
   {
      get{ check_index( ix ); return ms_textfiles[ix]; }
      set{ check_index( ix ); ms_textfiles[ix] = value;}
   }
}

A static method, static indexer, or static property cannot be declared as either virtual or abstract. The class constructors and destructor cannot be declared as virtual.

How many instances of the static ms_textfiles or ms_MaxFiles actually exist? Only one, regardless of how many classes we derive from the base class. Even after we provide the four derived-class instances of Query, there exists only one instance of each static or const data member of the base class.

We have declared the two static members as private. A private member of the base class cannot be directly accessed within the derived class. This means that each derived-class instance must go through the associated public static property or static index in order to read (or write) the member.

The benefit of a private base-class member is a loose coupling between the base class and subsequent derived classes. If we allow the derived classes direct access to a base-class member, any change in the implementation of that member is likely to break the implementation of those derived classes as well.

The potential drawback is unacceptable overhead in the performance of the derived classes because of the inability to directly access a critical base-class member. The solution in this case is to declare the member as protected, thereby allowing the derived class direct access to the member but still preventing the general user from having access.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.225.149.238