×

Loading...
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。

解答见内

本文发表在 rolia.net 枫下论坛[20.5] When should my destructor be virtual? Updated!
[Recently rewrote (in 9/02). Click here to go to the next FAQ in the "chain" of recent changes.]
When someone will delete a derived-class object via a base-class pointer.

In particular, here's when you need to make your destructor virtual:

if someone will derive from your class,
and if someone will say new Derived, where Derived is derived from your class,
and if someone will say delete p, where the actual object's type is Derived but the pointer p's type is your class.
Confused? Here's a simplified rule of thumb that usually protects you and usually doesn't cost you anything: make your destructor virtual if your class has any virtual functions. Rationale:

that usually protects you because most base classes have at least one virtual function.
that usually doesn't cost you anything because there is no added per-object space-cost for the second or subsequent virtual in your class. In other words, you've already paid all the per-object space-cost that you'll ever pay once you add the first virtual function, so the virtual destructor doesn't add any additional per-object space cost. (Everything in this bullet is theoretically compiler-specific, but in practice it will be valid on almost all compilers.)
Note: if your base class has a virtual destructor, then your destructor is automatically virtual. You might need an explicit destructor for other reasons, but there's no need to redeclare a destructor simply to make sure it is virtual. No matter whether you declare it with the virtual keyword, declare it without the virtual keyword, or don't declare it at all, it's still virtual.

BTW, if you're interested, here are the mechanical details of why you need a virtual destructor when someone says delete using a Base pointer that's pointing at a Derived object. When you say delete p, and the class of p has a virtual destructor, the destructor that gets invoked is the one associated with the type of the object *p, not necessarily the one associated with the type of the pointer. This is A Good Thing. In fact, violating that rule makes your program undefined. The technical term for that is, "Yuck."更多精彩文章及讨论,请光临枫下论坛 rolia.net
Report

Replies, comments and Discussions:

  • 工作学习 / IT技术讨论 / 下星期参加第二轮INTERVIEW,问个C++问题:"为什么将一个类的destructor设计成virtual function?",急!
    • 因为有继承,多态....基本概念之一...面试的时候这么简短回答可能也能过关...
      • 详细点?
        • 我给你找个例子去....
        • 解答见内
          本文发表在 rolia.net 枫下论坛[20.5] When should my destructor be virtual? Updated!
          [Recently rewrote (in 9/02). Click here to go to the next FAQ in the "chain" of recent changes.]
          When someone will delete a derived-class object via a base-class pointer.

          In particular, here's when you need to make your destructor virtual:

          if someone will derive from your class,
          and if someone will say new Derived, where Derived is derived from your class,
          and if someone will say delete p, where the actual object's type is Derived but the pointer p's type is your class.
          Confused? Here's a simplified rule of thumb that usually protects you and usually doesn't cost you anything: make your destructor virtual if your class has any virtual functions. Rationale:

          that usually protects you because most base classes have at least one virtual function.
          that usually doesn't cost you anything because there is no added per-object space-cost for the second or subsequent virtual in your class. In other words, you've already paid all the per-object space-cost that you'll ever pay once you add the first virtual function, so the virtual destructor doesn't add any additional per-object space cost. (Everything in this bullet is theoretically compiler-specific, but in practice it will be valid on almost all compilers.)
          Note: if your base class has a virtual destructor, then your destructor is automatically virtual. You might need an explicit destructor for other reasons, but there's no need to redeclare a destructor simply to make sure it is virtual. No matter whether you declare it with the virtual keyword, declare it without the virtual keyword, or don't declare it at all, it's still virtual.

          BTW, if you're interested, here are the mechanical details of why you need a virtual destructor when someone says delete using a Base pointer that's pointing at a Derived object. When you say delete p, and the class of p has a virtual destructor, the destructor that gets invoked is the one associated with the type of the object *p, not necessarily the one associated with the type of the pointer. This is A Good Thing. In fact, violating that rule makes your program undefined. The technical term for that is, "Yuck."更多精彩文章及讨论,请光临枫下论坛 rolia.net
          • 太好了,明白!
    • 因为是个BASE CLASS
    • 当该类作为基类且析构函数被调用时(如被delete),其derived类的析构函数会被调用。