Pointers and Objects Summary
You should note several points about using pointers to objects (refer to Figure 12.5 for a summary):
• You declare a pointer to an object by using the usual notation:
String * glamour;
• You can initialize a pointer to point to an existing object:
String * first = &sayings[0];
• You can initialize a pointer by using new
. The following creates a new object:
String * favorite = new String(sayings[choice]);
Also see Figure 12.6 for a more detailed look at an example of initializing a pointer with new
.
• Using new
with a class invokes the appropriate class constructor to initialize the newly created object:
// invokes default constructorString * gleep = new String;// invokes the String(const char *) constructorString ...
Get C++ Primer Plus 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.