Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Member variable of type std::array<T, ?>

Tags:

c++

arrays

oop

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?

like image 518
clstaudt Avatar asked Dec 18 '12 14:12

clstaudt


1 Answers

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.

like image 143
Björn Pollex Avatar answered Oct 03 '22 19:10

Björn Pollex