C ++对象数组没有<矢量> [英] C++, array of objects without <vector>

查看:83
本文介绍了C ++对象数组没有<矢量>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在C ++中不使用STL创建对象的数组。

I want to create in C++ an array of Objects without using STL.

我怎样才能做到这一点?

How can I do this?

我怎么能创造Object2的,它没有argumentless的构造函数(默认构造函数)的阵列?

How could I create array of Object2, which has no argumentless constructor (default constructor)?

推荐答案

如果有问题的类型有一个无参数的构造函数,使用新[]

If the type in question has an no arguments constructor, use new[]:

Object2* newArray = new Object2[numberOfObjects];

不要忘了叫删除[] 当你不再需要该数组:

delete[] newArray;

如果它不具有这样的构造函数中使用运营商新的来分配内存,然后调用就地构造函数:

If it doesn't have such a constructor use operator new to allocate memory, then call constructors in-place:

//do for each object
::new( addressOfObject ) Object2( parameters );

再次,不要忘记释放阵列时,你不再需要它。

Again, don't forget to deallocate the array when you no longer need it.

这篇关于C ++对象数组没有<矢量>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆