Creating and Destroying Objects

When coding using an object oriented language you define classes. During the execution of your program these class definitions define fields that contain the current state of each object belonging to that class as well as defining methods by which that state may be changed.

These methods are defined in a similar way to the way that we define functions in structured programming languages except that we can define two or more methods with the same name but with different parameters being passed to them in order to provide similar functionality for different arguments. These methods should be given meaningful names that describe what each method does with regard to the objects that it will act on.

There are two special cases where we need to define methods. These are the methods that will handle the initial creation of objects belonging to the class (which is known as the constructor) and also the one which will handle the removal of the object when we are finished with it (called the destructor).

One characteristic of objects that differentiates one object from another within the same class is what we call the state of the object. For example if the objects we are considering are electric lights then they can either be on or off. The constructor method is used to define the initial state of a newly created object. Does our new electric light bulb begin its life in the on state or the off state. Of course with the objects that you are defining in your program there will probably be a number of different aspects of the object that need to have their state defined when the object is first created and the settings for all of these aspects will need to be specified in the constructor. Even the information that is supplied when creating the object can vary (objects of the same type can be created in different ways) which is why we can have multiple constructor definitions for the same class of objects.

All of the objects that we create will eventually cease to exist. If the object is defined with local scope then it will automatically cease to exist when the code within which its declaration is contained completes execution and passes control back to a higher level. If the object is defined with global scope then it will cease to exist when the execution of the program finishes. Even where a persistent object is defined where the state of the object is stored between program executions there will eventually come a time where you want to delete the object. The purpose of the destructor method is to clean up any state information about the object being deleted.

Unlike the other methods defined for the class, these special methods are not explicitly called from within your program code but instead are called automatically when the object is first defined and when it finally passes out of scope. We can define multiple constructor methods supplying different lists of parameters for each and the one that gets executed to create the object will depend on the arguments supplied on the object declaration. We can only have one destructor method defined since in most cases there is not an explicit statement coded in your program that executes the destructor, instead it usually runs implicitly when the object declaration passes out of scope. The instance where we do need an explicit call to the destructor is when we have used an object declaration that is independent of the scope of the code in which it is declared.

Another way in which these methods differ from other methods is that they do need to have the value returned by the method defined. The constructor always returns a pointer to the object created while the destructor doesn't return anything at all.

To give an example of what this means let's imagine we have defined a class called myClass in C++. The class declaration is as follows:

class myClass {
myClass(int i, char j);
myClass(char k, char l);

The two constructor methods are defined to have the same name as the class to which they belong with only the parameters being passed to them differing. The destructor also has the same name but preceded by ~ and does not have any parameters.

We can create objects using these declarations as follows:

foo(i, j)
myClass x(i, j);

In this example the object is created by the myClass statement within the code and the object is given the unique name "x". Which of the two constructors is executed to create the object depends on how the fields i, and j passed to foo are declared. If they are both character fields then the second constructor will be used to create the object instead of the first even though the field names used match that of the first constructor. The destructor will be run automatically when the processing reaches the } statement within the code since this object is only defined within foo. As you can see since there is no explicit call to the destructor there is no opportunity to pass arguments to the destructor and hence it is not allowed to have any.

Now let's look at an example where the scope of the object created is not contained wholly within the scope of the code in which it is defined. C++ uses the new and delete statements to perform this functionality.

myClass *x;
foo(i, j)
x = new myClass(i, j);
delete x;

In this instance the object "x" is again created in foo just as before but this time the object will continue to exist after foo finishes execution. The way that this works is that we have defined a pointer with a more global scope that will point to "x" once it is defined within foo. This pointer will continue to point to the object until the object is explicitly selected. In our example this happens when the delete statement is executed in bar.


This article written by Stephen Chapman, Felgall Pty Ltd.

go to top

FaceBook Follow
Twitter Follow