Re: Multidimensional array member initialization
"io_x" <a@b.c.invalid> ha scritto nel messaggio
news:4b463e69$0$1105$4fafbaef@reader4.news.tin.it...
here is my solution (don't know if it is right)
this is better because all immage is one array without holes
#include <iostream>
#include <stdlib.h>
#define S sizeof
#define R return
using namespace std;
struct Color3d{
float r, g, b;
};
class Image{
public:
int height;
int width;
char* pxs;
Image(int height, int width);
~Image();
Color3d& v(int h, int w);
void fill(Color3d& color);
};
Color3d& Image::v(int h, int w)
{R (Color3d&) *(pxs+w*S(Color3d)+h*width*S(Color3d));}
Image::Image(int h, int w)
{int i, g;
height=0; width=0; pxs=0;
if(w<=0||h<=0) return;
g =h*sizeof(Color3d);
if(g<=0) return;
g*=w;
if(g<=0) return;
pxs=(char*) malloc(g);
if(pxs==0) return;
width=w; height=h;
}
Image::~Image(){free(pxs);}
void Image::fill(Color3d& color)
{int x, y;
if(pxs==0) return;
for(x=0; x<height; ++x)
{for(y=0; y<width; ++y)
(*this).v(x,y)=color;
}
}
int main(void)
{Color3d h={0.78373, 0.1383, 1-0.78373-0.1383};
Image g(768, 1024);
cout << "Inizio\n";
if(g.pxs==0)
{cout << "Error\n"; return 0;}
g.fill(h);
cout << "(r,g,b)==(" << g.v(0,0).r << ", "
<< g.v(0,0).g << ", "
<< g.v(0,0).b << ")\n";
cout << "end\n";
return 0;
}
----------
why is not possible to define:
Color3d& Image::operator[][](int i, int j)
{return (Color3d&) *(pxs+j*sizeof(Color3d)+i*sizeof(Color3d)*width);}