|
#include <stdio.h>
#include <tchar.h>
#include<windows.h>
class b
{
public:
int x;
b(){};
~b(){printf("对象被删除\n");} //至始至终没调用它
};
class a
{
public:
a *data(){return this+1;}
static a* Createit(a*& head,UINT m,UINT n);
void Freeit();
};
a *a::Createit(a*& head, UINT m, UINT n)
{
head=(a*)new BYTE[sizeof(a)+m*n];
return head;
}
void a::Freeit()
{
//a* p=this;
b *p1=new b();
delete p1;
}
int _tmain(int argc, _TCHAR* argv[])
{
a* p=a::Createit(p,5,sizeof(b));
b *b1=(b*)p->data();
b *b2=b1+1;
b1->x=5;b2->x=6;
printf("b1->x=%d,b2->x=%d\n",b1->x,b2->x);
p->Freeit(); //删除对象
printf("b1->x=%d,b2->x=%d\n",b1->x,b2->x);
getchar();
return 0;
} |
|