Create array of real C ++ objects

I want to create an array to store real objects, not pointers to objects in C ++?

Can someone please explain how should I do this? it's better to use vectors or just like this:

Student s [10];

      

OR

Student s [10][];

      

+3


source to share


3 answers


Using:

Student s [10];

      

Creates an array of 10 Student

instances.

I think it is Student s [10][];

invalid.

But with C ++ I would not use arrays of C types, it is better to use classes like std::vector

C ++ 0x std::array

or which may not be available with non-modern standard libraries / compilers.



Example for the above using std::vector

#include <vector>

...

std::vector<Student> students(10);

      

And with std::array

:

#include <array>

...

std::array<Student, 10> students;

      

+4


source


Don't use arrays. C arrays are not C ++. Use instead std::vector

, which is the C ++ way to do this.



+2


source


I would suggest using std :: vector if you want your array to be persistent, otherwise just use student-to-student [10]; for 10 objects.

+1


source







All Articles