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: direct invocation http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
   Static Class Members: Direct Invocation
   A  direct  invocation of a destructor requires the class  name  to  be
   specified as well as the destructor name, thus:

   ptr->Myclass::~Myclass();
   An Example
   In the Vector example, the destructor can be implemented inline in the
   class definition.

   class Vector {
   public:
   Vector();
   Vector(int n);
   Vector(vector& v);
   Vector(int a[], int n);
   ~Vector() { delete p; }
   // the destructor
   private:
   int* p;
   int size;
   };
   Arrays of Class Objects
   Arrays  of class objects are defined in the same way as an array of  a
   basic type. For example:

   Vector table[20];
   Vector *tabptr = new Vector[20];
   both define arrays of 20 Vectors.
   The  subscript  operator  []  can be used  to  access  the  individual
   elements  as normal. The following syntax is used to access the  class
   members of a particular element of the array:

   for (int i = 0; i  10; i++)
   cout << table[i].size;
   Elements in the array of class objects are initialized using the class
   constructors in the same way as individual objects. However there  are
   some restrictions. Array definitions of the type:

   Vector x[10];

   can only be used if the class has a default constructor defined for it
   (one that takes no arguments). The default constructor will be invoked
   for  any  array  element which is not  explicitly  initialized.  Class
   objects  which  are  elements  of  arrays  can  be  initialized  using
   constructors that take arguments by specifying the arguments for  each
   element in an array initialization list which is enclosed with braces.
   For example:

   Vector vec1[] = { 3, 5, 5, 4 };
   creates  an  array of 4 vectors of size 3, 5, 5  and  4  respectively,
   whereas:

   Vector vec2[6] = { 10, 8, 12 };
   creates an array of 6 vectors, three of which are initialized to sizes
   of  10, 8 and 12 elements and three of which are initialized with  the
   default constructor.

   An  array  of class objects which have been allocated  from  the  free
   store  using new cannot be initialized by the above syntax  and  must,
   therefore, either have no constructors, or have a default constructor.
   Such  an  array  must be removed by an explicit  call  to  the  delete
   operator:

   delete [elements] cptr;
   where  elements  is the number of elements specified in  the  original
   call  to  new and cptr is the pointer to the array of  class  objects.
   This ensures that the destructor is called for each object,  otherwise
   only the space allocated to the array is reclaimed.


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