#include<iostream>
#include<string>
using namespace std;
const int N=5;
class Frame
{
public:
Frame(string& s){str=s;print();}
void* operator new(size_t);
void operator delete(void*);
void print();
private:
string str;
};
Frame* f;
string s[N * sizeof(Frame)];
void* Frame::operator new(size_t size)
{
f = reinterpret_cast<Frame*>(s);//
//for(int i=0;i<N;i++)
//{
return f;
//}
}
void Frame::operator delete(void *p)
{
int j=static_cast<string*>(p)-s;//
j /= sizeof(Frame);
}
void Frame::print()
{
cout<<str<<" \n";
}
void main()
{
Frame *frame[N];
string name[N]={"s1","s2","s3","s4","s5"};
for(int i=0;i<N;i++)
{
cout<<"creat"<<i+1<<" ";
frame[i]=new Frame(name[i]);
}
for(int j=0;j<N;j++)
{
cout<<"delete"<<j+1<<" ";
delete frame[j];
}
}
在这里,为什么用f = reinterpret_cast<Frame*>(s);和int j=static_cast<string*>(p)-s;不用就错了,能说下这两句话在这段代码里的做用吗?