How do I declare and set a member variable for a template class AClass which is of type std::array<T, ?>
(with undefined size)? The actual std::array
is to be created in the constructor, where the size of the array is a constructor parameter.
In pseudo-C++ code:
template <typename T> class AClass {
protected:
std::array<T, ?>* array;
public:
AClass(int n) {
this->array = new std::array<T, n>;
}
}
How would correct code look like?
Don't use std::array
for that, use std::vector
. The size of an std::array
must be a compile-time constant. If you want to pass it in the constructor, you need to use a std::vector
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With