A DYNAMICALLY ALLOCATED OBJECT

Examine the file named OBJDYNAM.CPP for our first look at a dynamically allocated object. This is not any different than any other dynamically allocated object, but an example is always helpful.

In line 39 we declare a pointer to an object of type box and since it is only a pointer with nothing to point to, we dynamically allocate an object for it in line 44, with the object being created on the heap just like any other dynamically allocated variable. When the object is created in line 44, the constructor is called automatically to assign values to the two internal storage variables. Note that the constructor is not called when the pointer is declared since there is nothing to initialize. It is called when the object is allocated.

Reference to the components of the object are handled in much the same way that structure references are made, through use of the pointer operator as illustrated in lines 50 through 52. Of course you can use the pointer dereferencing method without the arrow such as (*point).set(12, 12); as a replacement for line 51 but the arrow notation is much more universal and should be used. Finally, the object is deleted in line 54 and the program terminates. If there were a destructor for this class, it would be called as part of the delete statement to clean up the object prior to deletion.

You have probably noticed by this time that the use of objects is not much different from the use of structures. Be sure to compile and execute this program after you have studied it thoroughly.

AN OBJECT WITH A POINTER TO ANOTHER OBJECT

The program named OBJLIST.CPP contains an object with an internal reference to another object of its own class. This is the standard structure used for a singly linked list and we will keep the use of it very simple in this program.

The constructor contains the statement in line 21 which assigns the pointer the value of NULL to initialize the pointer. This is a good idea for all of your programming, don’t allow any pointer to point off into space, but initialize all pointers to something. By assigning the pointer within the constructor, you guarantee that every object of this class will automatically have its pointer initialized. It will be impossible to overlook the assignment of one of these pointers.

Two additional methods are declared in lines 12 and 13 with the one in line 13 having a construct we have not yet mentioned in this tutorial. This method returns a pointer to an object of the box class. As you are aware, you can return a pointer to a struct in standard C, and this is a parallel construct in C++. The implementation in lines 48 through 51 returns the pointer stored within the object. We will see how this is used when we get to the actual program.

An extra pointer named box_pointer is declared in the main program for use later and in line 66 we make the embedded pointer within the small box point to the medium box, and in line 67 we make the embedded pointer within the medium box point to the large box. We have effectively generated a linked list with three elements. In line 69 we make the extra pointer point to the small box. Continuing in line 70 we use it to refer to the small box and update it to the value contained in the small box which is the address of the medium box. We have therefore traversed from one element of the list to another by sending a message to one of the objects. If line 70 were repeated exactly as shown, it would cause the extra pointer to refer to the large box, and we would have traversed the entire linked list which is only composed of three elements.

ANOTHER NEW KEYWORD this

Another new keyword is available in C++, the keyword this. The word this is defined within any object as being a pointer to the object in which it is contained. It is implicitly declared as;

class_name *this;

and is initialized to point to the object for which the member function is invoked. This pointer is most useful when working with pointers and especially with a linked list when you need to reference a pointer to the object you are inserting into the list. The keyword this is available for this purpose and can be used in any object. Actually the proper way to refer to any variable within a list is through use of the predefined pointer this, by writing this->variable_name, but the compiler assumes the pointer is used, and we can simplify every reference by omitting the

pointer. Use of the keyword this is not illustrated in a program at this point, but will be used in one of the larger example programs later in this tutorial.

You should study this program until you understand it completely then compile and execute it in preparation for our next example program.

Share
Tweet
Share
Pin