I would have liked to do something like this:
template<typename T>
class RasterView {
T* data;
unsigned stride;
RasterView(T* data, unsigned stride)
: data(data)
, stride(stride)
{}
public:
T& operator()(int j, int i) { // hot path
return data[static_cast<unsigned long>(stride) * i + j];
}
RasterView<T> subView(int j, int i) {
return RasterView<T>(data + static_cast<unsigned long>(stride) * i + j, stride);
}
// ...
}
template<typename T>
class Raster : public RasterView<T> {
std::vector<T> buffer;
public:
Raster(unsigned w, unsigned h, T defaultPixel)
: buffer(w*h, defaultPixel) // construct buffer first because we need...
, RasterView<T>(buffer.data(), w) // ... to pass this ptr to the base class constr
{}
// ...
}
but of course base class construction happens before member variable initialization, so the pointer buffer.data()
isn't available at the time the base class is being constructed. Does anyone have any advice or know of a trick I could use to work around this without adding too much complexity to the implementation?