Multi-dimensional arrays of objects

Hey guys

i want to make a two-dimensional array that is consisted by objects but i dont want to initialize the objects when i make the array.. i just want to make an array that each of his "cells" has the size of an object.

For example,

1
2
3
4
5
class Human{

int hm=5;
...
};


and now i want to make a two dimensional array that each "cell" has the size of an Human but not a Human itself.

Any ideas of how i make this?
Sounds like you're trying to build something like
std::array<unsigned char, sizeof(Human)> arr[3][3];
(that is, an array of cells, each the size of a Human), but I have a feeling that all you need is a std::vector<Human>. What is the use case for this?
oh.. i forgot to say that is forbidden for my project to use STL library...so i have to build up the array with pointers etc.

for example in C language, it would be something like that:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 
...
int** array;

 array=malloc(N * sizeof(int *));

 for(i = 0; i<N; i++){
    array[i]=malloc(N * sizeof(int));
 }


for(i=0; i<N; i++){
   for(j=0; j<N; j++){
     array[m][w]=0;
   }
 }

...



but i want to make something like this in C++ with objects , so something like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

Human** array;


array=new (N * sizeof(Human *));

 for(i = 0; i<N; i++){
    array[i]=new (N * sizeof(Human));
 }

Human human1;

array[0][1]=human1;

...



is this possible? is there a way to do it without using STL?
is my second code completely wrong?
Last edited on
Topic archived. No new replies allowed.