I want to create in C++ an array of Objects without using STL.
我想在c++中创建一个对象数组,而不用STL。
How can I do this?
我该怎么做呢?
How could I create array of Object2, which has no argumentless constructor (default constructor)?
我如何创建Object2的数组,它没有argumentless构造函数(默认构造函数)?
9 个解决方案
#1
33
If the type in question has an no arguments constructor, use new[]
:
如果所讨论的类型具有无参数构造函数,请使用new[]:
Object2* newArray = new Object2[numberOfObjects];
don't forget to call delete[]
when you no longer need the array:
当您不再需要数组时,不要忘记调用delete[]:
delete[] newArray;
If it doesn't have such a constructor use operator new
to allocate memory, then call constructors in-place:
如果它没有这样的构造函数使用操作符new来分配内存,那么就就地调用构造函数:
//do for each object
::new( addressOfObject ) Object2( parameters );
Again, don't forget to deallocate the array when you no longer need it.
同样,不要忘记在不再需要数组时重新分配数组。
#2
13
// allocate memory
Object2* objArray = static_cast<Object2*>( ::operator new ( sizeof Object2 * NUM_OF_OBJS ) );
// invoke constuctors
for ( size_t i = 0; i < NUM_OF_OBJS; i++ )
new (&objArray[i]) Object2( /* initializers */ );
// ... do some work
// invoke destructors
for ( size_t i = 0; i < NUM_OF_OBJS; i++ )
objArray[i].~Object2();
// deallocate memory
::operator delete ( objArray );
#3
11
Assuming that your class is Base and you have a one argument constructor
假设你的类是基类并且你有一个单参数构造函数
Base arr[3] = {Base(0), Base(1), Base(2)} ;
#4
5
Object2 *myArray[42];
for (int i = 0; i < 42; i++)
{
myArray[i] = new Object2(param1, param2, ...);
}
Later on you will have to walk through the array and deallocate each member individually:
稍后,您将必须遍历数组并逐个分配每个成员:
for (int j = 0; j < 42; j++)
{
delete myArray[j];
}
#5
3
Use an array of pointers to Object2:
使用指向Object2的指针数组:
std::tr1::shared_ptr<Object2>* newArray = new shared_ptr<Object2>[numberOfObjects];
for(int i = 0; i < numberOfObjects; i++)
{
newArray[i] = shared_ptr<Object2>(new Object2(params));
}
Or, alternatively, without the use of shared_ptr:
或者,不使用shared_ptr:
Object2** newArray = new Object2*[numberOfObjects];
for(int i = 0; i < numberOfObjects; i++)
{
newArray[i] = new Object2(params);
}
#6
3
You can do what std::vector
does and create a block of raw memory. You then construct your objects which don't have a default constructor in that memory using placement new, as they are required. But of course, if you do that you might as well have used std::vector
in the first place.
您可以执行std::vector执行并创建一个原始内存块。然后构造对象,这些对象在该内存中没有默认的构造函数,并使用placement new,因为它们是必需的。当然,如果你这么做的话,你最好先用std::vector。
#7
0
If no default constructor is available, you will need an array of pointers, and then loop over that array to initialize each of the pointers.
如果没有可用的默认构造函数,您将需要一个指针数组,然后循环该数组以初始化每个指针。
#8
0
The obvious question is why you don't want to use the STL.
显而易见的问题是为什么你不想使用STL。
Assuming you have a reason, you would create an array of objects with something like Obj * op = new Obj[4];
. Just remember to get rid of it with delete [] op;
.
假设您有一个原因,您将创建一个对象数组,对象的类型为Obj * op = new Obj[4];只需记住删除[]op;。
You can't do that with an object with no constructor that doesn't take arguments. In that case, I think the best you could do is allocate some memory and use placement new. It isn't as straightforward as the other methods.
如果对象没有不带参数的构造函数,就不能这样做。在这种情况下,我认为最好的办法是分配一些内存并使用新的放置。它不像其他方法那样简单。
#9
0
If you genuinely need an array (contiguous sequence of objects) of a non-default constructible type and for some reason you cannoy user std::vector
(!?) then you have to use a raw allocation function and placement new.
如果您确实需要一个非默认构造类型的数组(对象的连续序列),并且由于某些原因您无法使用std::vector(!?),那么您必须使用原始分配函数和新的放置。
This is very hard to do reliably; this should help to show why. This snippet includes some defence against exceptions but is more than likely not robust against all failures.
这很难做到可靠;这应该有助于说明原因。这段代码包含了一些针对异常的防御,但很有可能对所有失败都不够健壮。
const size_t required_count = 100; //e.g.
// cast to pointer of required type needed for pointer arithmetic
Object2* objarray = static_cast<Object2*>(operator new(required_count * sizeof(Object2)));
size_t construction_count = 0;
try
{
while (construction_count < required_count)
{
// params could change with index.
new (static_cast<void*>(objarray + construction_count)) Object2(param1, param2);
++construction_count;
}
}
catch (...)
{
while (construction_count-- != 0)
{
try
{
(&objarray[construction_count])->~Object2();
}
catch (...)
{
// not a lot we can do here, log but don't re-throw.
}
}
operator delete(objarray);
throw;
}
// Now objarray has been allocated and pointer to an array of required_count Object2
// It cannot be de-allocated via delete[] or delete; you must loop through
// calling destructors and then call operator delete on the buffer.
#1
33
If the type in question has an no arguments constructor, use new[]
:
如果所讨论的类型具有无参数构造函数,请使用new[]:
Object2* newArray = new Object2[numberOfObjects];
don't forget to call delete[]
when you no longer need the array:
当您不再需要数组时,不要忘记调用delete[]:
delete[] newArray;
If it doesn't have such a constructor use operator new
to allocate memory, then call constructors in-place:
如果它没有这样的构造函数使用操作符new来分配内存,那么就就地调用构造函数:
//do for each object
::new( addressOfObject ) Object2( parameters );
Again, don't forget to deallocate the array when you no longer need it.
同样,不要忘记在不再需要数组时重新分配数组。
#2
13
// allocate memory
Object2* objArray = static_cast<Object2*>( ::operator new ( sizeof Object2 * NUM_OF_OBJS ) );
// invoke constuctors
for ( size_t i = 0; i < NUM_OF_OBJS; i++ )
new (&objArray[i]) Object2( /* initializers */ );
// ... do some work
// invoke destructors
for ( size_t i = 0; i < NUM_OF_OBJS; i++ )
objArray[i].~Object2();
// deallocate memory
::operator delete ( objArray );
#3
11
Assuming that your class is Base and you have a one argument constructor
假设你的类是基类并且你有一个单参数构造函数
Base arr[3] = {Base(0), Base(1), Base(2)} ;
#4
5
Object2 *myArray[42];
for (int i = 0; i < 42; i++)
{
myArray[i] = new Object2(param1, param2, ...);
}
Later on you will have to walk through the array and deallocate each member individually:
稍后,您将必须遍历数组并逐个分配每个成员:
for (int j = 0; j < 42; j++)
{
delete myArray[j];
}
#5
3
Use an array of pointers to Object2:
使用指向Object2的指针数组:
std::tr1::shared_ptr<Object2>* newArray = new shared_ptr<Object2>[numberOfObjects];
for(int i = 0; i < numberOfObjects; i++)
{
newArray[i] = shared_ptr<Object2>(new Object2(params));
}
Or, alternatively, without the use of shared_ptr:
或者,不使用shared_ptr:
Object2** newArray = new Object2*[numberOfObjects];
for(int i = 0; i < numberOfObjects; i++)
{
newArray[i] = new Object2(params);
}
#6
3
You can do what std::vector
does and create a block of raw memory. You then construct your objects which don't have a default constructor in that memory using placement new, as they are required. But of course, if you do that you might as well have used std::vector
in the first place.
您可以执行std::vector执行并创建一个原始内存块。然后构造对象,这些对象在该内存中没有默认的构造函数,并使用placement new,因为它们是必需的。当然,如果你这么做的话,你最好先用std::vector。
#7
0
If no default constructor is available, you will need an array of pointers, and then loop over that array to initialize each of the pointers.
如果没有可用的默认构造函数,您将需要一个指针数组,然后循环该数组以初始化每个指针。
#8
0
The obvious question is why you don't want to use the STL.
显而易见的问题是为什么你不想使用STL。
Assuming you have a reason, you would create an array of objects with something like Obj * op = new Obj[4];
. Just remember to get rid of it with delete [] op;
.
假设您有一个原因,您将创建一个对象数组,对象的类型为Obj * op = new Obj[4];只需记住删除[]op;。
You can't do that with an object with no constructor that doesn't take arguments. In that case, I think the best you could do is allocate some memory and use placement new. It isn't as straightforward as the other methods.
如果对象没有不带参数的构造函数,就不能这样做。在这种情况下,我认为最好的办法是分配一些内存并使用新的放置。它不像其他方法那样简单。
#9
0
If you genuinely need an array (contiguous sequence of objects) of a non-default constructible type and for some reason you cannoy user std::vector
(!?) then you have to use a raw allocation function and placement new.
如果您确实需要一个非默认构造类型的数组(对象的连续序列),并且由于某些原因您无法使用std::vector(!?),那么您必须使用原始分配函数和新的放置。
This is very hard to do reliably; this should help to show why. This snippet includes some defence against exceptions but is more than likely not robust against all failures.
这很难做到可靠;这应该有助于说明原因。这段代码包含了一些针对异常的防御,但很有可能对所有失败都不够健壮。
const size_t required_count = 100; //e.g.
// cast to pointer of required type needed for pointer arithmetic
Object2* objarray = static_cast<Object2*>(operator new(required_count * sizeof(Object2)));
size_t construction_count = 0;
try
{
while (construction_count < required_count)
{
// params could change with index.
new (static_cast<void*>(objarray + construction_count)) Object2(param1, param2);
++construction_count;
}
}
catch (...)
{
while (construction_count-- != 0)
{
try
{
(&objarray[construction_count])->~Object2();
}
catch (...)
{
// not a lot we can do here, log but don't re-throw.
}
}
operator delete(objarray);
throw;
}
// Now objarray has been allocated and pointer to an array of required_count Object2
// It cannot be de-allocated via delete[] or delete; you must loop through
// calling destructors and then call operator delete on the buffer.