How to create objects if I dont know in prior how many to create.?For example I am creating an application and I dont know in prior how many users are going to use? So should I declare fixed number of objects(Say Myclass obj[200]) or can I dynamically create as and when I want or have a new user?
Asked
Active
Viewed 47 times
-1
-
2Use a `std::vector
` instead. – aschepler Apr 19 '17 at 04:18 -
2We've had `std::vector` and other collections for decades. [The Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – molbdnilo Apr 19 '17 at 04:18
-
make a linked list by hand -- well, actually bad idea, just go with two previous comments and use some libraries. – Bagus Tesa Apr 19 '17 at 04:32
2 Answers
1
You can use the NEW keyword to assignate the numberofusers (variable) needed
Myclass[] obj= new Myclass [NumberOfUsers];

Ricardo Ortega Magaña
- 2,403
- 16
- 23
0
I would suggest using a vector:
std::vector<Myclass> obj;
This way you can change its size quickly using:
vector::resize() and vector::reserve()

Kostas
- 4,061
- 1
- 14
- 32