Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++, array of objects without <vector>

Tags:

c++

arrays

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

How can I do this?

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

like image 826
osgx Avatar asked Mar 22 '10 15:03

osgx


1 Answers

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

Object2* newArray = new Object2[numberOfObjects]; 

don't forget to call delete[] when you no longer need the array:

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.

like image 197
sharptooth Avatar answered Oct 03 '22 20:10

sharptooth