CODE
#include <iostream.h>
class Cat
{
public:
Cat() { itsAge = 1; itsWeight = 5; }
~Cat();
int GetAge() const { return itsAge; }
int GetWeight() const { return itsWeight; }
void SetAge(int age) { itsAge = age; }
private:
int itsAge;
int itsWeight;
};
Cat::~Cat()
{
// cout << "Destructor called!\n";
}
int main()
{
Cat * Family = new Cat[10];
int i;
Cat * pCat;
for (i = 0; i < 10; i++)
{
pCat = new Cat;
pCat->SetAge(2*i + 1);
Family[i] = *pCat;
delete pCat;
}
for (i = 0; i < 10; i++)
{
cout << "Cat #" << i+1 << ": ";
cout << Family[i].GetAge() << endl;
}
delete [] Family;
return 0;
}
The author said that it is necessary to free the pointer after using it, to avoid creating memory leaks. My question is, after deleting Family, how am i gonna access the array on the free store again? Is it a wise move to release the Family pointer? Since then, if its not accessable again why not remove the whole array instead?
This post has been edited by lohmk: Feb 17 2006, 02:41 AM
Feb 17 2006, 02:21 AM, updated 20y ago
Quote
0.0148sec
1.52
5 queries
GZIP Disabled