6.3w+
社区成员
class A
{
public:
string str;
void clear(){ memset(this, 0, sizeof(*this);}
}
struct LAYERDATA
{
int a;
char b;
char c[20];
XStr str;
LAYERDATA() : a(0), b(0){}
void clear()
{
memset(this, 0, sizeof(*this));// 注意这里,原45楼代码取的是sizeof(this)
}
~LAYERDATA() { cout << "~LAYERDATA" ; }
};
#include "stdafx.h"
#include <cstdlib>
#include <iostream>
#include "windows.h"
using namespace std;
class A
{
public:
string str;
void clear(){ memset(this,0,sizeof(*this)); }
void foo(){ str += "cplusplus"; } //增加这样一个函数,它是内存安全的,只是为了防止对常量字符串作的优化
};
int main(int argc, char *argv[])
{
A a;
for(int i=0;i<1000000;++i)
{
a.str = "rick";
for(int j=0;j<100;++j)
{
a.foo();
}
a.clear();
Sleep(10);
cout<<".";
}
system("PAUSE");
return EXIT_SUCCESS;
}