• If an object is an automatic variable, the object’s destructor is called when the program exits the block in which the object is defined. Thus, in Listing 12.3 the destructor is called for headlines[0] and headlines[1] when the program exits main(), and the destructor for grub is called when the program exits callme1().
• If an object is a static variable (external, static, static external, or from a namespace), its destructor is called when the program terminates. This is what happened for the sports object in Listing 12.3.
• If an object is created by new, its destructor is called only when you explicitly use delete on the object.
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 constructor
String * gleep = new String;
// invokes the String(const char *) constructor
String * glop = new String("my my my");
// invokes the String(const String &) constructor
String * favorite = new String(sayings[choice]);
• You use the -> operator to access a class method via a pointer:
if (sayings[i].length() < shortest->length())
• You apply the dereferencing operator (*) to a pointer to an object to obtain an object:
if (sayings[i] < *first) // compare object values
first = &sayings[i]; // assign object address
Figure 12.5. Pointers and objects.
Figure 12.6. Creating an object with new.
Looking Again at Placement new
Recall that placement new allows you to specify the memory location used to allocate memory. Chapter 9, “Memory Models and Namespaces,” discusses placement new in the context of built-in types. Using placement new with objects adds some new twists. Listing 12.8 uses placement new along with regular new to allocate memory for objects. It defines a class with a chatty constructor and destructor so that you can follow the history of objects.
Listing 12.8. placenew1.cpp
// placenew1.cpp -- new, placement new, no delete
#include
#include
#include
using namespace std;
const int BUF = 512;
class JustTesting
{
private:
string words;
int number;
public:
JustTesting(const string & s = "Just Testing", int n = 0)
{words = s; number = n; cout << words << " constructed\n"; }
~JustTesting() { cout << words << " destroyed\n";}
void Show() const { cout << words << ", " << number << endl;}
};
int main()
{
char * buffer = new char[BUF]; // get a block of memory
JustTesting *pc1, *pc2;
pc1 = new (buffer) JustTesting; // place object in buffer
pc2 = new JustTesting("Heap1", 20); // place object on heap
cout << "Memory block addresses:\n" << "buffer: "