Re: Multidimensional array member initialization
On Jan 7, 10:40 am, "olivier.scalb...@algosyn.com"
<olivier.scalb...@algosyn.com> wrote:
Hello,
I would like to create an Image class. A use of this class could be
something as:
Image image1(1000, 1000);
Image image2(2000, 1000);
Color3d color;
image1.fill(color);
image2.fill(color);
Here is the code of the class:
struct Color3d
{
double r,g,b;
}
class Image
{
public:
Image(int width, int height);
~Image();
void fill(Color3d color);
private:
int _width;
int _height;
Color3d _pixels[][]; // Does not work !
};
Image::Image(int width, int height) : _width(width), _height(height)
{
_pixels = new Color3d[width][height]; // Does not work !
}
Image::~Image()
{
delete [][] _pixels; // Does not work
}
void Image::fill(Color3d color)
{
for (int y = 0; y < _height; y++)
{
for (int x = 0; x < _width; x++)
{
_pixels[x][y] = color;
}
}
}
As you can imagine, I have some problems around the pixels definition.
I see two alternatives:
- using templates;
- allocate a single dimension array of pixels and manage the
coordinates conversion myself.
I am sure there are some more elegant solutions, but I have no ideas.
No need to handle memory management yourself. vector is very good at
it.
Consider making the functions on Image free functions. That way, you
can
add functions later without changing the basic Image type.
Here's an example of how to use vector in combination with proxy
objects
to support array style [][] indexing.
Hope this helps.
Gert-Jan
#include <vector>
template <typename T>
class Image
{
class Indexer
{
public:
Indexer(T* data) : data_(data)
{
}
T& operator[](int x) const
{
return data_[x];
}
private:
T* data_;
};
class ConstIndexer
{
public:
ConstIndexer(const T* data) : data_(data)
{
}
T operator[](int x) const
{
return data_[x];
}
private:
const T* data_;
};
public:
Image(int width, int height) :
width(width),
height(height),
data(width*height)
{
}
int width() const
{
return width_;
}
int height() const
{
return height_;
}
Indexer operator[](int y)
{
return Indexer(&data_[y*width_]);
}
ConstIndexer operator[](int y) const
{
return ConstIndexer(&data_[y*width_]);
}
private:
int width_;
int height_;
std::vector<T> data_;
};
struct Color3d
{
double r, g, b;
};
void fill(Image<Color3d>& img, Color3d color)
{
for (int y = 0; y < img.height(); ++y)
for (int x = 0; x < img.width(); ++x)
img[y][x] = color;
}