Page 137 - DCAP404 _Object Oriented Programming
P. 137

Object-oriented Programming




                    Notes          6.7 Constructor/ Destructor with Static Members

                                   A CLR type, such as a class or struct, can have a static constructor, which can be used to initialize
                                   static data members. A static constructor will be called at most once, and will be called before the
                                   first time a static member of the type is accessed.
                                   An instance constructor will always run after a static constructor.
                                   The compiler cannot inline a call to a constructor if the class has a static constructor. The compiler
                                   cannot inline a call to any member function if the class is a value type, has a static constructor,
                                   and does not have an instance constructor. The common language runtime may inline the call,
                                   but the compiler cannot.
                                   A static constructor should be defined as a private member function, as the static constructor is
                                   only meant to be called by the common language runtime.
                                   To get the equivalent of a static constructor, you need to write a separate ordinary class to hold
                                   the static data and then make a static instance of that ordinary class.

                                   class  StaticStuff
                                   {
                                             std::vector<char>  letters_;
                                   public:
                                             StaticStuff()
                                        {
                                            for (char c = ‘a’; c <= ‘z’; c++)
                                                 letters_.push_back(c);
                                        }
                                             //  provide  some  way  to  get  at  letters_
                                   };
                                   class  Elsewhere
                                   {
                                       static StaticStuff staticStuff; // constructor runs once, single instance
                                   };


                                          Example:
                                   class  A  {

                                   int  a;
                                   public:
                                   A(int  i)  {a=i;}
                                   ~A()  {cout  <<  “bye\n”;}
                                   };
                                   A  x(3);  //  Static  constructor
                                   main()  {

                                   //  do  stuff..




          130                               LOVELY PROFESSIONAL UNIVERSITY
   132   133   134   135   136   137   138   139   140   141   142