C++ Primer(P347):定位new操作符用法?
#include<iostream>
#include<new>
using namespace std;
const int chunk=16;
class TFoo
{
public:
int val(){return _val;};
TFoo(){_val=1;};
private:
int _val;
};
//预分配内存,但没有TFoo对象
char *buf=new char[sizeof(TFoo)*chunk];//这里用int等类型,不影响结果。为什么?
int main()
{
//在buf中创建一个TFoo对象
TFoo *pb=new(buf) TFoo;
//检查一个对象是否被放在buf中
if(pb->val()==1)
cout<<"new expression worked!"<<endl;
delete[] buf;
return 0;
}
上面已经有一个问题了,还有,有谁知道定位new操作符有咱们什么实际的用途啊?