AI Features

Discussion: A Destructive Relationship

Execute the code to understand the output and gain insights into destructors and virtual functions.

Run the output

Now, it’s time to execute the code and observe the output.

C++ 17
#include <iostream>
#include <memory>
struct Widget
{
virtual void draw() { std::cout << "Widget draw\n"; }
virtual ~Widget() { std::cout << "Widget destructor\n"; }
};
struct Button : public Widget
{
void draw() override { std::cout << "Button draw\n"; }
~Button() override { std::cout << "Button destructor\n"; }
};
int main()
{
std::unique_ptr<Widget> widget = std::make_unique<Button>();
widget->draw();
}

Understanding the output

...
Ask