How do I instantiate an array's size later?
c++
Solution
If you want to use a C-style array, the size must be fixed and known at compile-time. And even in that case, you could use the safer, zero-overhead `std::array<>` wrapper instead.
If the size of your container is not known at compile-time, then it is good practice to use `std::vector` (or `std::deque` in some cases, based on your requirements in terms of memory allocation) and avoid manual memory management through raw pointers, `new[]` and `delete[]`:
#include <string> // For std::string
#include <vector> // For std::vector
class Base {
public:
std::vector<std::string> myVector;
};
Besides, this design won't require any dedicated work in the constructor (and destructor) of `Derived`. If all that was done by `Derived`'s default constructor was to allocate the array, now you can avoid explicitly defining a default constructor at all, and let the compiler generate one for you implicitly - same story for the destructor.
Also, I would discourage you from using names of standard container classes (like `array`) as names for your variables. Something like `myArray` (or `myVector`, as in my example above) are more appropriate choices.
Problem
Let's say I have a base class called ``` Class Base { public: std::string array[]; }; ``` The size the string array is not decided until another class extends it, what's the correct syntax for doing so? EG, later on in a derived class ``` Derived::Derived() { array[] = new array[40]; } ```