Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Zortech C++ Language Reference - static_class_members _ http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
                           Static_Class_Members                       _
   A  static class member is a class member which is shared  between  all
   the objects of that class. It is created by prefixing its declaration,
   inside  the  class  definition,  with the  keyword  static.  Only  one
   instance  of  a  static class member is created  no  matter  how  many
   objects of that class are created. If any one object alters the  value
   of  the  static member, it is reflected by all objects.  Static  class
   members obey the rules for data hiding in relation to whether they are
   declared  in  the public, protected or private section  of  the  class
   definition.  For this reason the use of static class members is to  be
   preferred to the use of non-member global variables.

   Static  class members are available whenever the class definition  for
   their class is in scope, even if no objects of that class exist.  They
   are  initialized  in  one of two ways, either as  part  of  the  class
   definition:

   class Abc {
   static int def;
   }
   int Abc::def = 5;// initialized here
   or separately with the implementation code for the class:
   #include <Abc.hpp>
   int Abc::def = 5;

   The  latter is the preferred method since, as with  global  non-member
   variables, only one initialization of a static member is allowed.

   A  static class member can be initialized regardless of its status  as
   public, protected or private.

   Because an instance of the static member exists even when there are no
   objects of the class, static members which are public can be  accessed
   without  reference  to a class object. This is done by  means  of  the
   class name and the scope resolution operator:

   if (Abc::def == 5)
   // take some action
   This  class  scope  operator must be used because  the  static  member
   exists with class scope, not global scope.

                          Static Member Functions
   The  fact  that  the same static data member  is  shared  between  all
   objects  of a class could lead to confusion where the  program  syntax
   makes the static member appear to be a normal member:

   Myclass ob1, ob2;
   // stat is a static class member
   // norm is a normal (non static) member
   ob1.stat = 1;
   ob2.stat = 2;
   ob1.norm = 1;
   ob2.norm = 2;

   In  this  case it appears as though we are dealing with  two  separate
   instances  of  stat  as  we are with norm,  but  in  fact  the  second
   assignment  to  stat  is  overwriting the  first,  i.e.  ob1.stat  and
   ob2.stat are both 2. This confusion can become even worse where access
   functions are involved since then you do not even have the name of the
   data item to remind you that it is static.

   Static  member functions provide a way of avoiding this  ambiguity.  A
   static  member function is indicated by prefacing the  declaration  of
   the function, within the class definition, with the keyword static. It
   is  a  function  which can only be used  for  accessing  static  class
   members.  It cannot access normal class members, and does not  have  a
   this  pointer.  Any  attempt  to access a  normal  class  member  will
   generate a compile time error. A static member function does not  need
   to be invoked by a class object. It can be used even if no objects  of
   the  class  have been defined. The syntax for using  a  static  member
   function read for class Abc would be:

   class Abc
   {
   public:
   static int read(void) { return def; }
   private:
   static int def;
   int ghi;
   };

   x = Abc::read();
   Static  member functions remain available while the  class  definition
   remains in scope.
   Pointers to Class Members

   In C++, a pointer to a function cannot be assigned to the address of a
   class member function even if the return type and parameter list match
   exactly. The reason for this is that a member function must operate on
   an  instance of its class. In order to assign the address of a  member
   function  to a function pointer, the pointer must have the same  class
   as the member function. This is also true when assigning a pointer  to
   a  member data item. The correct way of defining a pointer to a  class
   member is to use the class name and the scope resolution operator.  In
   the definition

   int Abc::*x;
   x is a pointer to an int member of class Abc, whereas in
   int (Abc::*x)(void);
   x  is  a  pointer to a member function of class  Abc  which  takes  no
   arguments and returns an int.

   Pointers  to  class members can only be accessed  through  a  specific
   object  of  the  class  via  the  syntax  .*  for  class  objects  and
   references,  and  ->*  for  pointers to class  objects.  They  do  not
   override the access controls within the class.

   A pointer to a member function is invoked as follows:

   int (Abc::*mp)() = Abc::set;

   //assign address of set function to mp

   Abc obj, *obp;

   int x = obj.set();
   //direct invocation
   int y = obp->set();
   //direct invocation by pointer

   x = (obj.*mp)();
   //pointer invocation
   y = (obj->*mp)();
   //pointer invocation by pointer

   The  parentheses  are  required because the  precedence  of  the  call
   operator  ()  is higher than the precedence of the pointer  to  member
   selection operator.

   A  similar syntax is used for a pointer to a data member  except  that
   the parentheses are not required:

   int Abc::ptr;

   Abc obj, *obp;

   int x = obj.def;
   //direct access
   int y = obp->def;
   //direct invocation by pointer

   x = obj.*ptr;
   //pointer access
   y = obj->*ptr;
   //pointer access by pointer


Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson