View Antoni Milton's profile on LinkedIn

Wednesday, August 24, 2011

Call the destructor more than one time

we can call the destructor as like function, but we can't call the constructor like this.

class Shape
{
   public:
       Shape(){ cout<<" Shape"<
       virtual ~Shape(){ cout<<" ~  Shape"<
};
class Circle : public Shape
{
   public:
      Circle() { cout<<"Circle"<< endl; }
      ~Circle(){ cout<<" ~ Circle" << endl; }
};

int main()
{

   Circle *ptr = new Circle();
   ptr->~Circle();
   ptr->~Circle();

   return 1;
}

output:-

# ./a.out
 Shape
Circle
 ~ Circle
 ~  Shape
 ~  Shape

while calling the destructor function first time , its calling the base call destructor also ( if its virtual ) . but after that not calling the base class destructor.

No comments: