Introduction

 < Day Day Up > 



Mastering the concept and use of pointers is crucial to understanding just about all other aspects of the C++ language. An understanding of pointers is required to really understand what’s going on in arrays, dynamic object creation, function argument passing by reference, iterators, and a whole host of other language topics.

Pointers are easy to learn but can be confusing at first. I think the reason for this is that C++ uses the same operator, like the asterisk * for instance, for several purposes. The purpose you’re most familiar with at this point is multiplication. But the asterisk is also used to declare and dereference pointer variables. The concept of using an operator for more than one purpose in C++ is referred to as operator overloading and is done to keep the language small and manageable. The compiler knows how to deal with overloaded operators based upon what context the operator appears. This is convenient for the compiler and the compiler writer but confuses the novice to no end. You’ll learn more about overloaded operators in chapter 14.

The question students ask when they first encounter pointers is, “OK...but what are they good for?” That’s a fair question. Pointers let you write leaner, meaner code. Pointers, combined with dynamic object instantiation, (creating objects at runtime using the new operator) let you conserve memory space, creating objects only when needed in a program. Pointers let you do things faster. An example of this can be found in sorting operations where the objects being compared are large. The objects themselves can remain in their original memory locations and only their addresses need be manipulated during the sort operation. Pointers let you pass the address of a large object to a function for processing without moving the object itself. Additionally, an understanding of pointers is key to understanding more complex data structures like linked lists and trees.

The purpose of this chapter, then, is to give you a solid understanding of pointers, what they are, how to declare them, how to dereference them, how to dynamically create objects in memory using the new and delete operators, and how to determine the address of objects in memory. I do not cover all the different ways to use pointers in this chapter because doing so would require discussing aspects of the language to which you have not yet been exposed. An example of this would be the use of pointers with functions which is left to chapter 9.



 < Day Day Up > 



C++ for Artists. The Art, Philosophy, and Science of Object-Oriented Programming
C++ For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504028
EAN: 2147483647
Year: 2003
Pages: 340
Authors: Rick Miller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net