anon
anon

Reputation:

Creating array of objects without specified size

I want to make an array of objects made from class, but i don't know how many of those objects will be in the array. I tried to make it with this code: MyClass a[];, but Qt Creator shows me an error: flexible array member 'a' of type 'MyClass []' with non-trivial destruction and a warning: flexible array members are a C99 feature. The code works with MyClass a[n]; but that is not what i need.

Upvotes: 1

Views: 1936

Answers (1)

chinagar
chinagar

Reputation: 136

In C++ you can make usage of vector<MyClass>a. Whenever you need to add any more object to the array use: a.push_back(object)

Basically when you declare a vector, a fix capacity container is created dynamically. Whenever the size of vector crosses the capacity, the entire vector is copied to a new location with increased size.

Upvotes: 2

Related Questions