博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Effective C++ 笔记
阅读量:7229 次
发布时间:2019-06-29

本文共 1426 字,大约阅读时间需要 4 分钟。

无参构造函数如何暴躁的构造.

先来看看标准的raw_memory :

void *intRawVar = operator new[](5*sizeof(int)); // create int raw memoryint *intVars = static_cast
(intRawVar); // let int object to point this raw memoryfor(int i=0;i<5;i++){ new(&intVars[i])int(i*10);}for(int i=0;i<5;i++){ cout << intVars[i] <

 

无参数如何暴力构造

#include 
class EquipmentPiece{public: EquipmentPiece(int IDNumber){ _var = IDNumber; } ~EquipmentPiece() { }private: int _var;};using namespace std;int main(int argc, char *argv[]){ //<1> //EquipmentPiece piece[10]; //error not default constructor //<2> typedef EquipmentPiece* PEP; //this method is ok,have resource leak & big memory allocation PEP piece[10]; // create ten EquipmentPiece* pointer //PEP *piece = new PEP[10]; // always is ok for(int i=0;i<10;i++) { piece[i] = new EquipmentPiece(i); } for(int i=0;i<10;i++) { delete piece[i]; } void *rawMemory = operator new[](10*sizeof(EquipmentPiece)); EquipmentPiece *pieces = static_cast
(rawMemory); for(int i=0;i<10;i++) { new(&pieces[i])EquipmentPiece(i); } // release the EquipmentPiece object with reverse direction for(int i=9;i >=0;--i) { pieces[i].~EquipmentPiece(); } // release the raw memory operator delete[](rawMemory); return 0;}

 

转载于:https://www.cnblogs.com/gearslogy/p/6395806.html

你可能感兴趣的文章
Node入门教程(10)第八章:Node 的事件处理
查看>>
组策略应用之一:映射网络驱动器
查看>>
程序复杂度之圈复杂度
查看>>
【入门须知】学DIV CSS技术如何入门?学DIV CSS技术入门
查看>>
flask 中从eml中获取头邮件头信息
查看>>
laravel上传文件&获取请求实例(隐式)
查看>>
适配低内存手机让app存在多个进程
查看>>
BW中DSO的分类
查看>>
echo -en
查看>>
Mysql 复制(Replication)实现
查看>>
我的友情链接
查看>>
jar生成exe可执行的程序
查看>>
date 转化为 指定格式的String
查看>>
使用virtualbox安装RHEL 6.2+Oracle 11g
查看>>
系统的域名服务
查看>>
MySQL 复制表结构
查看>>
《Effective C#》条款8:确保0为值类型的有效状态
查看>>
动态迁移应用服务器(Esxi 动态迁移技术,业务不间断,在线迁移)
查看>>
systemd coding style
查看>>
warning: control reaches end of non-void function
查看>>