How do virtual destructors work?

后端 未结 2 664
情书的邮戳
情书的邮戳 2020-12-17 02:51

I am using gcc. I am aware how the virtual destructors solve the problem when we destroy a derived class object pointed by a base class pointer. I want to know how do they w

2条回答
  •  有刺的猬
    2020-12-17 03:53

    The key thing you need to know is that not using a virtual destructor in the above code is undefined behavior and that's not what you want. Virtual destructors are like any other virtual functions - when you call delete the program will decide what destructor to call right in runtime and that solves your problem.

提交回复
热议问题