Arrays of Objects

 

"Should array indices start at 0 or 1? My compromise of 0.5 was rejected without, I thought, proper consideration."

 
 --Stan Kelly-Bootle

An array is an indexed collection of objects. Each object in an array has the same type.


	ElementType theArray[numberofElements];

	Account allTheAccounts[2000];
	

Each element is initialised by calling the default constructor. If there is no default constructor, we could use (provided we use the correct number of elements):


int main()
{
  Account someAccounts[3] = {
        Account("Tom", 20.00, 123456),
        Account("Richard", 55.00, 123457),
        Account("Harry", 99.00, 123458) };  1
  Account *p = &someAccounts[0]; 2

  someAccounts[2].display();    //displays Harry 3

  p->display();		    //displays Tom 4
  (++p)->display();     //displays Richard 5
  (p+1)->display();     //displays Harry 6
	
  //Warning! The pointer p now points at Richard, not Tom! 7
}

The full source code for this example is listed in AccountArrayExample.cpp.

1

The array has defined and initialised 3 elements, by calling their constructors. The someAccounts array has 3 elements (0 to 2).

2

The pointer p is set to point at the address of the first element in the someAccounts array - element 0.

3

The array can be indexed directly using the someAccounts array variable, so someAccounts[2] is a reference to the last element in the array and can be operated on in the same way as any other object, using the . operator.

4

The pointer p is currently pointing at the first element in the array, someAccounts[0] and when display() is called on the pointer p the details of Tom's account will be displayed.

5

The pointer p has been pre-incremented, and now points at the second element in the arrray someAccounts[1]. If (p++)->display(); had been used instead, then the post-increment would have incremented the index after the display() was called, and so it would have displayed "Tom's" details instead of "Richard's" details. Note that the pointer index has been incremented by one and now points directly at the second element in the array.

6

This call displays the details of the last element in the array "Harry's" details, as the pointer p points at the second element in the array someAccounts[1] , but is further offset by one (not incremented by 1!).

7

The pointer remains pointing at the second element in the array someAccounts[1]. It is important to use incrementing and offsetting correctly when using pointers, otherwise your applications will not work correctly.

Pointers can be used to allocate arrays of objects using the new keyword.


  Account *ptr;
  ptr = new Account[10];

A pointer to an array of objects has the exact same form as a pointer to an object. There is no way to distinguish between a pointer to a type and a pointer to an array of that type, except by careful coding. To destroy the array use delete[] ptr; If ptr points to an array then delete ptr; is undefined. Here is an example of using delete[] on an array of your own objects: using_delete.cpp