In this chapter we discussed pointers. Pointers are indirect references, useful when data need to be accessed in more than one way and when data must be allocated dynamically. We discussed sharing...


In this chapter we discussed pointers. Pointers are indirect references, useful when data need to be accessed in more than one way and when data must be allocated dynamically. We discussed sharing objects between classes using reference variables and pointers. We also discussed self-referential data structures called linked lists that have many applications. It’s possible to insert new elements into a linked list without shifting the existing elements, making linked lists the method of choice for many sparse structures. We studied copy constructors, assignment operators, and destructors, three member functions often required when instance variables point to objects on the heap. We also saw an example of designing, implementing, and testing a templated class by starting with a nontemplated class.


Topics covered include:


Variables have names, values, and addresses. The address of a variable can be


assigned to a pointer.


As part of defensive programming, make pointers point to objects allocated on the


heap using new, not to objects allocated on the stack.


Several operators are used to manipulate pointers: ->, *, &, and operators


new, and delete.


Pointers can be used for efficiency since a tvector of pointers to strings requires less space than a tvector of strings, especially if the tvector is not full.


The new operator is used to allocate memory dynamically from the heap. Memory can be allocated using new in conjunction with a constructor with arguments.


Pointers are dereferenced to find what they point to. Pointers can be assigned values in four ways: using new, using & to take the address of existing storage (not a good idea, in general), assigning the value of another pointer, and assigning 0 or NULL.


A destructor member function is called automatically when an object goes out of scope. Any memory allocated using new during the lifetime of the object should be freed using delete in the destructor.


Reference instance variables can be used to share an object among more than one object. Reference instance variables must be initialized at construction; once constructed and bound to an object, a reference variable cannot be bound to a different object (unlike a pointer, for example.)


Pointers can be used to change the values of parameters indirectly. This is how parameters are changed in C: addresses are passed rather than values. The indirect addresses are used to change values.


Linked lists support splicing, or fast insertions and deletions (in contrast to vectors in which items are often shifted during insertion and deletion). However, items near the end of a linked list take more time to access than items near the front.


Recursive linked list functions (sometimes with pointers passed by reference) are often shorter than an equivalent iterative version of the function.


A header node can be used when implementing linked lists to avoid lots of specialcase code, especially when deleting and inserting elements.


Doubly and circularly linked lists are alternatives to singly linked lists.


Classes can be templated so that they can be used to generate literally thousands of different classes, just as templated functions represent thousands of functions.

Nov 22, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here