返回   cpper编程论坛 > C/CPP/TMP/GP
注册账号 论坛帮助 会员列表 日历事件 搜索 今日新帖 标记版面已读

回复
 
LinkBack 主题工具 显示模式
  #1 (permalink)  
旧 2008-07-18
liuxinyu 的头像
高级会员
 
注册日期: 2006-02-09
帖子: 303
文章: 48
liuxinyu 正向着好的方向发展
默认 Pure virtual dtor

刚才我同事问我的一个问题,分享一下:
Typically if a class has a pure virtual dtor, you can’t create instance of this class. It is designed as an abstract class.

But you must provide a default implementation for that class, or the compiler will generate error:

Example:

c++ 代码:
  1. class Base{
  2. public:
  3.   virtual ~Base() = 0;
  4. };
  5.  
  6. Base::~Base(){
  7. }
Without default dtor implementation, following errors will be rasied:
undefined reference to `Base::~Base()`

You must over write the dtor in derived classes as the following:
c++ 代码:
  1. #include <iostream>
  2.  
  3. class Base{
  4. public:
  5.   virtual ~Base() = 0;
  6. };
  7.  
  8. Base::~Base(){
  9. }
  10.  
  11. class Foo: public Base{
  12. public:
  13.   ~Foo(){
  14.     std::cout<<"Foo closed\n";
  15.   }
  16. };
  17.  
  18. class Bar: public Base{
  19. public:
  20.   ~Bar(){
  21.     std::cout<<"Bar closed\n";
  22.   }
  23. };
  24.  
  25. int main(int, char**){
  26.   //Base* p=new Base; this won't pass compile
  27.   Base* f=new Foo;
  28.   Base* b=new Bar;
  29.   delete f;
  30.   delete b;
  31. }

And the result is as below:
liuuuxin@WEIFANG ~/temp
$ g++ test.cpp -o test

liuuuxin@WEIFANG ~/temp
$ ./test
Foo closed
Bar closed

顺便引一下标准:
The standard says in section 12.4 [class.dtor] para 7 page 192:
"A destructor can be declared virtual (10.3) or pure virtual (10.4) ..."
__________________
==================================
http://liuxinyu95.googlepages.com
liuxinyu95@gmail.com
==================================
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #2 (permalink)  
旧 2008-07-18
bankrock 的头像
高级会员
 
注册日期: 2003-12-11
帖子: 843
文章: 7
bankrock 正向着好的方向发展
默认 回复: Pure virtual dtor

这没什么疑问吧,确实是这样的,Effectiv C++ Item14最后讲过这个问题。
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #3 (permalink)  
旧 2008-07-20
cat cat 当前离线
高级会员
 
注册日期: 2003-11-06
帖子: 1,560
文章: 6
cat 正向着好的方向发展
默认 回复: Pure virtual dtor

是闹明白了以后的总结吧?我也没看到问题。
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
回复

书签

主题工具
显示模式

发帖规则
不可以发表新主题
不可以发表回复
不可以上传附件
不可以编辑自己的帖子

启用 BB 代码
论坛启用 表情符号
论坛启用 [IMG] 代码
论坛禁用 HTML 代码
Trackbacks are 启用
Pingbacks are 启用
Refbacks are 启用


相似的主题
主题 主题作者 版面 回复 最后发表
谁说C++没有foreach? Unsigned C/CPP/TMP/GP 13 2006-07-12 07:17 PM
捕捉鼠标输入的方法 bankrock 技术杂烩 33 2005-06-16 03:32 PM
练习三:大家都知道virtual function,怎样实现virtual data? abp 技术杂烩 18 2004-07-02 05:49 PM
多重继承其中一个基类是virtual继承. andy84920 技术杂烩 7 2004-03-01 03:26 PM
容器中放入同一個template class產生的對象? sad 技术杂烩 9 2003-03-26 09:40 PM


所有时间均为格林尼治时间 +9。现在的时间是 08:34 AM


Powered by vBulletin® 版本 3.7.0
版权所有 ©2000 - 2008,Jelsoft Enterprises Ltd.
(C) Copy Right All Right Reserved 2001 - 2007

Search Engine Friendly URLs by vBSEO 3.1.0