Chapter 23

Pointers to Objects

In This Chapter

arrow Defining pointers to objects

arrow Invoking a member function through a pointer

arrow Passing pointers to objects to functions

arrow Examining this more closely

Chapters 17 and 18 focus on various aspects of the care and feeding of pointers. Surely, you think, nothing more can be said on the subject. But I don’t introduce the concept of classes before those chapters. In this chapter, I describe the intersection of pointer variables and object-oriented programming. This chapter deals with the concept of pointers to class objects. I describe how to create one, how to use it, and how to delete it once you’re finished with it.

Pointers to Objects

A pointer to a programmer-defined type such as a class works essentially the same as a pointer to an intrinsic type:

  int nInt;int* pInt = &nInt;class Savings{  public:    int nAccountNumber;    double dBalance;};Savings s;Savings* pS = &s;

The first pair of declarations defines an integer, nInt, and a pointer to an integer, pInt. The pointer pInt is initialized to point to the integer nInt.

Similarly, the second ...

Get Beginning Programming with C++ For Dummies, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.