If you can't place a hard limit on the size of the array at compile time, then you'll need to dynamically allocate it.
Are you allowed to ask the user how many cities they want, and then allocate the array to a specific size? If not, then you'll have to keep re-allocating new memory for the array when a new city as added, and then copy the contents of the old, smaller array, into the new, larger one. It might be more efficient to do this in blocks of, say, 10 objects, rather than for every new object.
Do you need to store City objects in the array? Or can you just store pointers to them? If you need to store actual objects, then you'll be copying them into the array, which means you'll need to worry about deep-copying the City object. You'll need to create a copy constructor for your class, and make sure that it properly copies the
name string rather than just copying the pointer.
Thanks i will try this but i dont understand what size_t is, i checked the documentation and dont really know. |
[...]
cause right now it sais "type name is not allowed" on the size_t part |
size_t is a type. It's almost always an integer of some kind. The code kefin posted isn't legal C++. I think what he was trying to say that you need to specify the size of the array as an integer.