C ++ removing an inherited class

Let's say there is an Object class, and then another Cat class that inherits Object. Next, there is a list of objects * (pointers). Then I create a new Cat and put it on the list. After some time, I want to delete all cats and delete delete for each member of the list. Does it call a Cat destructor?

+5
source share
4 answers

Yes, if you marked the object destructor as virtual.

class Object {
  public:
  virtual ~Object(){} //make the base class destructor virtual
};

class cat : public Object {
  public:
  virtual ~cat(){} // now this gets called when a pointer to Object that is a cat is destroyed
}
+17
source

If the object's destructor is virtual, yes.

+7
source

, , - , . ++ FAQ.

+3

, class Object ( ) , undefined, .

0

All Articles