declaration for array of objects

hi
i already declared array of object
Author arrA[]={A1,A2,A3};
and i initialized it with 3 objects ,
in my program new Author object may be added to the array during the execution
it work correctly and accept the new objects , but there is something wrong in the output , when i try to print the content of the array .
1
2
for(int a=0;a<sizeof arrA/ sizeof arrA[0];a++)
      cout<<arrA[a].A_ID <<endl;


actually i dont wanna fixed size for my array , but im forced to initialize it at the beginning .

how to do ? any idea ?
Arrays are fixed size. You better use an std::vector

1
2
3
4
5
6
7
std::vector<Author> arrA;
arrA.push_back(A1);
arrA.push_back(A2);
arrA.push_back(A3);

for (std::size_t a = 0; a < arrA.size(); a++)
      cout<<arrA[a].A_ID <<endl;


In C++11 you can write it a bit more compact:
1
2
3
4
std::vector<Author> arrA{A1,A2,A3};

for (Author& a : arrA)
      cout<< a.A_ID <<endl;

Last edited on
Thank u very much Peter87
it solved my problem.
i think i must read more about vectors , its useful more than array .
Topic archived. No new replies allowed.