首页 > 其他 > 详细

new操作符

时间:2014-06-29 19:14:39      阅读:321      评论:0      收藏:0      [点我收藏+]

1,new操作符实际上包含三部分:operator new分配内存和调用构造函数初始化刚刚分配的内存,类型转换刚刚的指针。

string* ps = new string("lalalala");

相当于

void* memory = operator new(sizeof(string));

call string::string("lalalala") on memory; //only compiler could do this step

string* ps = static_cast<string*>(memory);

 

2,placement new可以在一个已经分配的内存上构造一个对象,placement new也会调用operator new,但是此时的operator new只是简单的返回传递给placement new的地址

  new(buffer)widget(widgetsize);

3, operator new和operator delete必须一一对应

  void* buffer = operator new(10*sizeof(char));

  operator delete(buffer);

4, 使用placement new必须包含<new>头文件

 

new操作符,布布扣,bubuko.com

new操作符

原文:http://www.cnblogs.com/dracohan/p/3810828.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!