Discussion: A Destructive Relationship
Learn how C++ handles the destruction of derived and base class objects through virtual destructors. Understand why destructors call in a specific order, the role of subobjects, and how RAII techniques like unique_ptr help manage resources safely during object destruction.
We'll cover the following...
We'll cover the following...
Run the output
Now, it’s time to execute the code and observe the output.
Understanding the output
When we call draw(), the program only prints Button draw. But when unique_ptr goes out of scope, and the destructor is called, the program prints both Button destructor and Widget destructor. Why is that?
Virtual functions
A common technique for dynamic polymorphism is using virtual functions. We ...