Let’s say I have a class that has a member which is an array. Is it possible to define its size upon construction/at run-time, in the following way:
class myClass {
private:
int myArray[n]
public:
myClass();
someOtherMethod();
};
Where n is a variable that is defined based on user input. If not, what would be the best alternative?
Use a vector.
Then, you can fill in the vector as you would an array. Alternatively, as Nawaz points out, use
reserve(), which reserves space for new elements, and/orpush_back(), which adds elements onto the back, one at a time.