posts - 9, comments - 0, trackbacks - 0, articles - 0
  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理
1, Declare destructors virtual in polymorphic base classes
why ? because C++ specifies that when a derived class object is deleted through a pointer to a base class with a non-virtual destructor, results are undefined.What typically happens at runtime is that the derived part of the object is never destroyed

2, if a class is not intended to be a base class, making the destructor virtual is usually a bad idea. 
why?  if a class have virtual functions, it has extra overhead(vptr).

3, In fact, many people summarize the situation this way: declare a virtual destructor in a class if and only if that class contains at least one virtual function

4,Sometimes, however, you have a class that you'd like to be abstract, but you don't have any pure virtual functions.
solution: declare pure virtual destructor.
There is one twist, however you must provide a definition for the pure virtual destructor, or linker will complain.

5, Not all base classes are designed to be used polymorphically. Neither the standard string type, for example, nor the STL container typesare designed to be base classes at all, much less polymorphic ones.


 
 

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理