Part 3: All About Virtual Keyword in C++: How Does Virtual Destructor Works?

Finally, we are in the 3rd & last part of this series. We have already discussed how virtual function & virtual class/inheritance works internally in previous parts. We left one topic i.e. “How Does Virtual Destructor Works?” which we will see now. As usual, before learning anything new I usually start with “Why Do We Need It in the First Place?” Why Do We Need a Virtual Destructor? We will understand this with our earlier example(slightly twisted): 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 class protocol_t { private: uint8_t *_type; // storage ....

September 12, 2019 · 6 min · 1194 words · Vishal Chovatiya