object in existence after deletion : delete « Development « C++ Tutorial






#include<iostream.h>
class MyClass
{
  static int total;
public:
  MyClass()
  {
    total++;
  }
  ~MyClass()
  {
    total--;
  }
  int gettotal()
  {
    return total;
  }
};
int MyClass::total=0;

main()
{
  MyClass o1,o2,o3;
  cout<<o1.gettotal()<<" objects in existence\n";
  MyClass *p;
  p=new MyClass;
  if(!p)
  {
    cout<<"Allocation erroe\n";
       return 1;
  }
  cout<<o1.gettotal();
  cout<<" object in existence after allocation\n";
  delete p;
  cout<<o1.gettotal();
  cout<<" object in existence after deletion\n";
  return 0;
}
3 objects in existence
4 object in existence after allocation
3 object in existence after deletion








5.13.delete
5.13.1.Memory management new-delete
5.13.2.Delete an array of objects
5.13.3.Global delete
5.13.4.object in existence after deletion
5.13.5.delete class array and return memory to heap
5.13.6.Using new and delete of array