类对象数组初始化(三种⽅法)
参考⾃《More Effective C++中⽂版》
类对象数组初始化参考⾃《More Effective C++中⽂版》
类对象数组初始化
如有⼀个如下类:
class EquipmentPiece {
private:
int IDNumber;
public:
EquipmentPiece(int IDNumber) : IDNumber(IDNumber) {};
};
以下列出⼏种初始化的⽅法:
<⼀>、对象数组
int ID1, ID2, ID3;
EquipmentPiece bestPieces[] = { EquipmentPiece(ID1), EquipmentPiece(ID2), EquipmentPiece(ID3) };
注意:
EquipmentPiece bestPieces[10]; //no appropriate default constructor available
EquipmentPiece *bestPieces = new EquipmentPiece[10]; //no appropriate default constructor available
当然,如果你将构造函数参数全都设了默认值,以上两种写法也成功,如将类中构造函数修改如下:
...
EquipmentPiece(int IDNumber = 0) : IDNumber(IDNumber) {};
...
<⼆>、指针数组
typedef EquipmentPiece* PEP; //PEP是个指向EquipmentPiece的指针
PEP bestPieces[10]; //等同于 PEP *bestPieces = new PEP[10];
//然后初始化
for(int i = 0; i < 10; i++){
bestPieces[i] = new EquipmentPiece( IDNumber );
}
注意:
要记得将此数组所指的所有对象删除。如果忘了会产⽣资源泄露。还有就是该⽅法与对象数组相⽐需要额外内存⽤于存放指针。(过度使⽤内存 这⼀问题可以避免,见第三种⽅法)
<;三>、使⽤placement new
⽅法是:先为此数组分配raw memory,然后使⽤"placement new"在这块内存上构造EquipmentPiece objects;
//分配⾜够的raw memory,给⼀个预备容纳10个EquipmentPiece objects的数组使⽤
void *rawMemory = operator new(10*sizeof(EquipmentPiece));
//让bestPieces指向此内存,使这块内存被视为⼀个EquipmentPiece数组
EquipmentPiece *bestPieces = reinterpret_cast<EquipmentPiece*>(rawMemory);
//利⽤"placement new"构造这块内存中的EquipmentPiece objects。
int IDNumber = 0;
for(int i = 0; i < 10; i++){
new (&bestPieces[i]) EquipmentPiece( IDNumber );
}
注意:该⽅法维护⽐较困难。在数组内对象结束⽣命时,要以⼿动⽅式调⽤destructors,最后还得调⽤operator delete释放raw memory。
//将bestPieces中对象以构造次序的反序析构掉
for(i = 0; i < 10; i++){
bestPieces[i].~EquipmentPiece();
}
//释放raw memory
operator delete (rawMemory);
如有⼀个如下类:
class EquipmentPiece {
private:
int IDNumber;
public:
EquipmentPiece(int IDNumber) : IDNumber(IDNumber) {};
};
以下列出⼏种初始化的⽅法:
<⼀>、对象数组
int ID1, ID2, ID3;
EquipmentPiece bestPieces[] = { EquipmentPiece(ID1), EquipmentPiece(ID2), EquipmentPiece(ID3) };
注意:
EquipmentPiece bestPieces[10]; //no appropriate default constructor available
EquipmentPiece *bestPieces = new EquipmentPiece[10]; //no appropriate default constructor available
当然,如果你将构造函数参数全都设了默认值,以上两种写法也成功,如将类中构造函数修改如下:
...
EquipmentPiece(int IDNumber = 0) : IDNumber(IDNumber) {};
...
<⼆>、指针数组
typedef EquipmentPiece* PEP; //PEP是个指向EquipmentPiece的指针
PEP bestPieces[10]; //等同于 PEP *bestPieces = new PEP[10];
//然后初始化
for(int i = 0; i < 10; i++){
bestPieces[i] = new EquipmentPiece( IDNumber );
}
注意:
要记得将此数组所指的所有对象删除。如果忘了会产⽣资源泄露。还有就是该⽅法与对象数组相⽐需要额外内存⽤于存放指针。(过度使⽤内存 这⼀问题可以避免,见第三种⽅法)
<;三>、使⽤placement new
⽅法是:先为此数组分配raw memory,然后使⽤"placement new"在这块内存上构造EquipmentPiece objects;
//分配⾜够的raw memory,给⼀个预备容纳10个EquipmentPiece objects的数组使⽤
void *rawMemory = operator new(10*sizeof(EquipmentPiece));
//让bestPieces指向此内存,使这块内存被视为⼀个EquipmentPiece数组
EquipmentPiece *bestPieces = reinterpret_cast<EquipmentPiece*>(rawMemory);
//利⽤"placement new"构造这块内存中的EquipmentPiece objects。
int IDNumber = 0;
for(int i = 0; i < 10; i++){
new (&bestPieces[i]) EquipmentPiece( IDNumber );
}
定义数组初始化注意:该⽅法维护⽐较困难。在数组内对象结束⽣命时,要以⼿动⽅式调⽤destructors,最后还得调⽤operator delete释放raw memory。
//将bestPieces中对象以构造次序的反序析构掉
for(i = 0; i < 10; i++){
bestPieces[i].~EquipmentPiece();
}
//释放raw memory
operator delete (rawMemory);
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论