虚函数,在主函数中的调用有错
#include<iostream.h>class A
{
public:
virtual ~A()
{
cout<<"调用A::~A()"<<endl;
}
};
class B:public A
{
char *buf;
public:
B(int i){ buf=new char[i]; }
virtual ~B()
{
delete [] buf;
cout<<"调用B::~B()"<<endl;
}
};
void fun(A *pa)
{
delete pa;
}
void main()
{
A *pa;//当主函数这样时,显示与下面不一样
B b(10);
pa=&b;
fun(pa);
//......................
A *pa=new B(10);
fun(pa);
}