Arrays of object types

in my program a series of classes one for data one for a table, the table class is trying to initialise an array of the data objects.

in the header i have specified a pointer:

Data *tableArray;

in the cpp classes constructor i have:

1
2
3
4
5
 tableArray = new Data[size];  

 for (int i=0; i < size; i++){
   tableArray[i] = new Data();
 }


this generates a number of errors, i assume it's something to do with how I am initialising the object but the constructor works fine when called from single context.

is there anything i'm missing to getting the array initialised?

any help is appreciated.
new Data[size] creates size instances of Data and calls the standard constructor for each.
There is no need to initialize anything as they're already initialized. And you can't assign Data pointers to Data objects anyway.
It's not an array of pointers, it's an array of objects of type Data. When you say new Data[size]; the default constructor is being called to all the objects allocated.

Notice that:

tableArray[i] = new Data();

is trying to assign a pointer to Data to an object of type Data (not Data*).

EDIT: or what Athar said :)
Last edited on
Thanks i thought you had to make the manual call or the pointer would just have space for the object but no actual object.

thanks for the help it seems to be working now.
Topic archived. No new replies allowed.