Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
为什么我们可以有一个虚拟析构函数而不是虚拟构造函数?
构造函数链可以在编译时确定,因为您new ClassName()总是使用最具体的类。
new ClassName()
但是,如果您使用多态性,您可能会在可能的父类上调用析构函数,因此您无法在编译时知道从哪里开始链。您需要一个虚函数来始终调用正确的函数(否则您最终会在最特定的类中获得可能未清理的资源)。