Learning  C++
Home
Tutorials
C++  Programs
Contact  us
Sitemap
Your Ad Here
Your Ad Here
Initialization  of  Constant  and  Reference  Data  Members

Without  using  the  member   initialization  list,  it  is  not  possible  to  initialize  the  const and reference  data  members.



For  example,  
                                                          class  A
                                                          {
                                                                         public :

                                                                                          A ( int  n )
                                                                                          {
                                                                                                     d2  = n ; // error3
                                                                                                     r = v ;
                                                                                          }

                                                                         private :

                                                                                          const  int  d1 = 15 ;  //  error2
                                                                                          const  int  d2 ;
                                                                                          int  v ;
                                                                                          int  &  r ;  // error1

                                                           } ;



The  error1  is:   the  reference  member  ' r '  is  not  initialized,  the  error2  is:   cannot  initialize a classs  member  here   and  the  error3  is:  cannot  modify  a  const  object.  These  three errors   can  be  removed  by   using  the  member  initialization  list.



                                                           class  A
                                                           {
                                                                      public :

                                                                                  A ( int  n ) : d1 ( 15 ) , d2 ( n ) , r ( v )
                                                                                  {
               
                                                                                  }

                                                                      private :

                                                                                  const  int  d1 ;
                                                                                  const  int  d2 ;
                                                                                  int  v ;
                                                                                  int  &  r ;

                                                           } ;



An  alternative  to  constant  members  is  to  use  an  anonymous  enumerator  containing  the item like  this:



                                              class  A
                                               {

                                                           public :

                                                                       enum  { size = 10 } ;
                                                                       int  vector [ size ] ;  // ok
                                                                       //  . . . . . .
                                               } ;



Reference  is  a  constant  Reference

A  reference  variable  is  conceptually  analogous  to  a  pointer  variable  in  that  they  both  refer or  point,  to  some  other  object.  Unlike  the  pointer,  however,  a  reference  must  be  initialized and  once  initialized,  a  reference  cannot  be  refer  to  other  object.  It  implies  that  a reference   is,  in  fact,  a  constant  reference.  There  is  no  need  to  explicitly  declare   constant  reference.

For  example,  the  statement   int &  const  m = v ;
is  always  equivalent  to  int &  m = v ;