Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

To correct this example, the destructor for Base should be declared virtual. This ensures that the object b refers to will be correctly evaluated at runtime thus, deleting b will call the destructor for class Derived.

Code Block
classcclass Base {
public:
   Base() {
      // Build Base object
   }
   virtual ~Base() {
      // Destroy Base object
   }
};

class Derived : public Base {
public:
   Derived() {
      // Build Derived object
   }
   ~Derived() {
      // Destroy Derived object
   }
};

void mainfunction(void) {
   Base* b = new Derived();
   // ...
   delete b;
}