在类中动态创建和删除内嵌类对象 为什么不改变类的大小 (内嵌类没有虚函数)
#include "stdafx.h"
#include <windows.h>
// Two class :
// One contained a class in itself and have a variable object of that class
// Two contained a same struct class and dynamic create a object of that class
// Aim :
// Test the space and time spent to the two kind
// By the way , to test a array of a class for how to get the size
class COne
{
public:
int count;
class CTest
{
public:
char chArray[1000000];
};
CTest* CreateTest()
{
CTest* pTest= new CTest;
printf("Create new Test object!\n");
printf("and ths size of this is %d\n",sizeof(this));
return pTest;
}
bool SetContent()
{
CTest* ptest = CreateTest();
for (int i=0; i<10000; i++)
{
ptest->chArray[i] = 'a';
}
ptest->chArray[i] = '\0';
printf("and ths size of this is %d\n",sizeof(this));
delete ptest;
return true;
}
};
int main(int argc, char* argv[])
{
COne one;
printf("The size of COne before dynamic create object is %d\n\n",sizeof(one));
one.SetContent();
printf("The size of COne after dynamic create object is %d\n\n",sizeof(one));
return 0;
}