
Mail Pointer Array
By: a guest on
Apr 29th, 2012 | syntax:
C++ | size: 1.08 KB | hits: 18 | expires: Never
#include <iostream>
using namespace std;
class Mail
{
public:
Mail() {}
Mail(int c) { cost = c;}
int getCost() { return cost; }
private:
int cost;
};
int main (int argc, const char * argv[])
{
const int MAX_MAIL = 20;
// Make an array of MAX_MAIL Mail pointers. The () at the end sets the array
// elements to 0 (i.e. null pointers). You could always iterate over the
// pointers and set them to 0 instead.
Mail **m = new Mail*[MAX_MAIL]();
// Create MAX_MAIL new Mail items, and assign their pointers to the array.
for (int i = 0; i < MAX_MAIL; i++)
m[i] = new Mail(i);
// Do stuff with the array here........
// Go through all pointers, delete them if they're not null.
for (int i = 0; i < MAX_MAIL; i++)
{
if (m[i] != 0)
{
delete m[i];
m[i] = 0;
}
}
// Delete the dynamically allocated array. This differs to deleting the
// individual Mail* array elements.
delete[] m;
return 0;
}