pure virtual function call

2015-04-08 10:58:19

基类中定义了纯虚函数,派生类中将其实现。

如果在基类的构造函数或者析构函数中调用了该纯虚函数,

则会出现R6205 Error: pure virtual function call

对象在构造时,会先调用基类构造函数,但此时派生类对象还未构造成功,

因此调用的纯虚函数的虚表指针指向基类的虚表,而基类的纯虚函数没有定义。

如果是在基类的析构函数中调用,此时的派生类已经被销毁,也会出现这种情况。

 1 #include <stdio.h>
 2 
 3 class Base
 4 {
 5 public:
 6     Base()
 7     {
 8         printf("Base construction be called...\n");
 9         open_func();
10     }
11 
12     void open_func()
13     {
14         printf("Base::open_func() be called...\n");
15         show();
16     }
17 
18     virtual void show() = 0;
19 };
20 
21 class D : public Base
22 {
23 public:
24     D()
25     {
26         printf("D construction be called...\n");
27     }
28 
29     void show()
30     {
31         printf("D::show() be called...\n");
32     }
33 };
34 
35 int main()
36 {
37     D d;
38     return 0;
39 }

posted @ 2015-04-08 10:59  felove  阅读(1943)  评论(0编辑  收藏  举报