static in C++
Learning  C++
Home
Tutorials
C++  Programs
Contact  us
Sitemap
Your Ad Here
Your Ad Here
Static  Variables

The  storage  class  of  a  variable  determines  which  parts  of  the  program  can  access it  and  how  long  it  stays  in  existence.  Here  we  are  concerned  with  static  automatic variables.   A   static   automatic  variable  has  the  visibility  of  a  local  variable  ( that  is, inside  the  function  containing  it ).  Its  lifetime  is  similar  to  that  of  an  external variable, except  that  it  doesn't  come  into  existence  until  the  first  call to  the  function containing it.  Thereafter  it  remains  in  existence  for  the  life  of  the  program.

Static  automatic  variables  are  used  when  it's  necessary  for  a  function  to  remember a  value  when  it  is  not  being  executed;  that  is,  between  calls  to  the  function.



                                   int  sum ( int ) ;    // declaration

                                   int  main ( )
                                   {
                                              int  data,  total ;
                                              do
                                              {

                                                           cin  >> data ;
                                                           total = sum ( data ) ;
                                                           cout  <<  " New  total  is "  <<  total  <<  endl ;

                                              } while ( data != 0 ) ;
                                   }


                                   int  sum ( int  d )
                                   {
                                               static  int  newtotal = 0 ;
                                               newtotal  + = d ;
                                               return  newtotal ;
                                   }



The  static  variable  newtotal  retain  its  value  after  sum ( )   returns  and  available  for the next  time.

When  static  variables  are  initialized,  the  initialization  takes  place  only  once --  the first time  their  function  is  called.  They  are  not  reinitialized  on  subsequent  calls  to the function,  as  ordinary  automatic  variables  are.



Static  Data  Members

A  data  member  of  a  class  can  be  qualified   as  static.  A  static  member  variable has certain  special  characteristics:

1)   Only  one  copy  of  that  member  is  created  for  the  entire  class  and  is  shared  by  all  the   objects  of  that  class,  no  matter  how  many  objects  are  created.

2)   It  is  visible  only  within  the  class,  but  its  lifetime  is  the  entire  program.

3)   It  is  initialized  to  zero  when  the  first  object  of  its  class  is  created.  No  other  initialization  is  permitted.

Static  variables  are  normally  used  to  maintain  values  common  to  the  entire  class. For  example,  a  static  data  member  can  be  used  as  a  counter  that  records  the occurrences  of  all  the  objects.



                                          class  A
                                          {
                                                     static  int  count ;

                                                     public :
                                                                  void  display ( void )
                                                                  {
                                                                              count ++ ;
                                                                              cout  <<  count  ;
                                                                  }
                                          } ;

                                          int  A :: count  ;

                                          int  main ( )
                                          {
                                                      A   a, b, c ;

                                                      a.display (  ) ;
                                                      b.display (  ) ;
                                                      c.display (  ) ;

                                                      a.display (  ) ;
                                                      b.display (  ) ;
                                                      c.display (  ) ;
                                          }



The  output  would  be  1 2 3 4 5 6




Static  Member  Functions

A  member  function  that  is  declared  as static  has  the  following  properties

1)   While  a  static  function  is  a  member  function,  it  does  not  contain  a  this  pointer,  thus  it  behaves  very  differently  than  a  normal  function.  It  can  manipulate  only  static  data  members  of  the  class;  it  cannot  access  non-static  members  of  the  class.

2)   Since  it  is  instance - independent,  it  can  be  called  directly  by  using  the  class  name  and  the  scope  resolution  operator ( assuming  that  it  is  public ).

3)   It   may  be  called  via  some   instance  of  the  class,   or  even  by  an  uninitialized  pointer  of  the  class   type,  but  this  is  not  recommended.


To  make  a  member  function  static,  precede  its  return  type  with  the  keyword  static.


                           
                              class  A
                              {
                                                  static  int  value ;

                                          public :

                                                  void  A  ( void )
                                                  {
                                                               ++ value ;
                                                  }

                                                  static  void  display ( void )
                                                  {
                                                            cout  << value ;
                                                  }
                              } ;



                              int  A : :  value = 0 ;

                              int  main ( )
                              {
                                          A  : :  display ( ) ;
                                          A   a,  b,  c ;
                                          A  : :  display ( ) ;
                                          return  0 ;
                              }


The  output  is  0  3