在这段代码中:
#include<iostream>
using namespace std;
class B
{
int b;
public:
~B(){ cout <<"B::~B()"<<endl; }//1
};
class D: public B
{
int i,d,e,f;
public:
~D() { cout <<"D::~D()"<<endl; }//2
};
int main(void)
{
cout << "sizeB:" << sizeof(B) << " sizeD:"<< sizeof(D) <<endl;
B *pb = new D[2];
delete [] pb;
return 0;
}
一开始,我不知道 delete[] 如何正常工作。然后我注意到这一点:
B* pb = new D[2];
&pb[1] - &pb[0] == sizeof(B);
D* pd = new D[2];
&pb[1] - &pb[0] == sizeof(D);
编译器做了什么?为什么它会这样工作?