I have some custom objects, using a simplified example to show the problem I am having
#include <vector>
struct DataPoint {
int x = 0;
int y = 0;
}
// The goal of this object is to look like a vector but without the
// dynamic allocation (after initial initialization) hence why I am
// forcing a constructor like this.
class DataPoints {
public:
explicit DataPoints(size_t n) : points_(n) {};
private:
size_t current_size_ = 0;
std::vector<DataPoint> points_;
};
class DataLayers {
public:
// Here is where I have the problem, there is no default constructor for DataPoints,
// and need to initialize layers_ which is an array. I hope to eventually allow
// layers_ to be different sizes on initialization and so can't hard code an array
// initialization
DataLayers() :
// What I would like to do (this does not work) is something like this, and each
// array element in layers_ gets initialized to a size of 100000;
layers_(100000),
// Have also tried
layers(DataPoints(100000)) {
}
private:
std::array<DataPoints, 10> layers_;
}
I have got a temporary work around by setting a default argument in DataPoints
DataPoints(size_t n = 100000) // rest of code
I would prefer to move the initialization/sizing to Layers since Layers knows more about how many DataPoints it needs. Is this possible in C++ (using 14).
layers_DataPointsinstance to come from? It has to come from somewhere.layers_astd::vectorinstead of astd::arrayanyway, and then there won't be any issue. Astd::vectorcan always be default-constructed.DataPoints(250)(for example)