Effective C++ 条款七 为多态基类声明virtual析构函数
2014-08-15 16:42
417 查看
class TimeKeeper
[code] {
public:
TimeKeeper();
// ~TimeKeeper(); 错误,此作为一个基类,被继承了。其继承类被delete后,基类被销毁,但继承类可能没被销毁
virtual ~TimeKeeper();//必须声明为virtual类型才可以。
protected:
private:
};
class AtomicClock: public TimeKeeper{}; //继承
TimeKeeper* ptk = getTimeKeeper();
delete ptk;
[/code]
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
记住:
1、带多态性质的基类应该声明为一个virtual析构函数,如果class带有任何virtual函数,他就应该拥有一个virtual析构函数。
2、classes设计的目的如果不是作为基类使用,或者不是为了具备多态性,就不应该声明为virtual类型的析构函数。
相关文章推荐
- Effective C++学习7 条款07:为多态基类声明virtual析构函数
- effective c++ 条款7,8(为多态基类声明virtual析构函数,别让异常逃离析构函数)
- 读书笔记《Effective c++》 条款07 为多态基类声明virtual析构函数
- Effective C++学习笔记 条款07:为多态基类声明virtual析构函数
- Effective C++:条款07:为多态基类声明virtual析构函数
- Effective C++学习笔记 条款07:为多态基类声明virtual析构函数
- effective c++ 条款07(为多态基类声明virtual析构函数)整理
- Effective C++ 条款7——为多态基类声明virtual析构函数
- 《effective C++》:条款07——为多态基类声明virtual析构函数
- 读书笔记《Effective C++》条款07:为多态基类声明virtual析构函数
- 《Effective C++》学习笔记条款07 为多态基类声明virtual析构函数
- [Effective C++]条款07 为多态基类声明virtual析构函数
- effective C++ 读书笔记 条款07 为多态基类声明virtual析构函数
- Effective C++_笔记_条款07_为多态基类声明virtual析构函数
- Effective C++--条款07:为多态基类声明virtual析构函数
- Effective C++ 条款07:为多态基类声明virtual析构函数
- 《Effective C++》条款07:为多态基类声明virtual析构函数
- Effective c++(第三版) 条款7:为多态基类声明virtual析构函数
- effective C++ 07_为多态基类声明virtual析构函数 读书笔记
- 《Effect C++》学习------条款07:为多态基类声明virtual析构函数