- 论坛徽章:
- 0
|
源代码如下:
#include <iostream>
using namespace std;
class CA{
public:
int m;
CA(int r=0){m=r;printf("%d CA::CA()\n",m);}
~CA(){printf("CA::~CA()\n");}
void f(){printf("%d CA::f()\n",m);}
};
class CB:public CA{
public:
CA mal;
CB():mal(2){printf("%d CB::CB()\n",m);}
~CB(){printf("CB::~CB()\n");}
void f(){printf("%d CB::f()\n",m);}
};
void testConstructor(){
CA *pCA=new CB();
pCA->f();
delete pCA;
}
void main()
{
testConstructor();
}
基类析构函数应该定义成虚的,但不是虚的,只是无法调用派生类析构函数。但如果只将派生类CB的析构函数定义为虚,则程序崩溃,请问这是为什么呢? |
|