Re: Multidimensional array member initialization
----------
"Gert-Jan de Vos" <gert-jan.de.vos@onsneteindhoven.nl> ha scritto nel messaggio
news:4a41375a-4445-4ffb-bf67-ebd403d600fe@j5g2000yqm.googlegroups.com...
Hope this helps.
Gert-Jan
#include <vector>
etc
---------
i have copy above code adding only some macro,
add 3 "_" in the constructor Image(int, int),
and the main() function for debug.
not understand why img.width()
is called each cicle of the inner loop; why not use
just x<img.width_?
yes i not have understood all but seems to me there
is something wrong g[768][1024].b
not seg fault, so for me could be something wrong.
than for me the centre of the exercise is:
immage has to be one big array without holes in it.
Can some of you could say "vector" operate in one big array
without holes in it?
-----------------------
#include <iostream>
#include <cstdlib>
#include <stdint.h>
#include <vector>
using namespace std;
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;
}
int main(void)
{Color3d h={0.78373, 0.1383, 1-0.78373-0.1383};
Image<Color3d> g(768, 1024);
cout << "Inizio\n";
fill(g, h);
cout << "(r,g,b)==(" << g[0][0].r << ", "
<< g[0][0].g << ", "
<< g[768][1024].b << ")\n";
cout << "end\n";
return 0;
}
---------
this below is the rewrite of above in one readable form;
with the same error, in one inesistent position of the array
the [768][124] say the same value of all other (1-0.78373-0.1383)
and not seg fault.
instead my example seg fault if [768][1024]; it
return 0 [and not (1-0.78373-0.1383)] for [767][1024]
better!
---------
#include <iostream>
#include <cstdlib>
#include <stdint.h>
#include <vector>
using namespace std;
template <typename T> class Image{
public:
int width_;
int height_;
std::vector<T> data;
class Indexer{
public:
T* data;
Indexer(T* dat) : data(dat){}
T& operator[](int x){return data[x];}
};
Image(int w, int h): width_(w), height_(h), data(w*h){}
int width() {return width_;}
int height() {return height_;}
// img[y] is Indexer
// where is the memory of Indexr? It is created and destroy?
// img[y][x] is T&
Indexer operator[](int y)
{return Indexer( & data[y*width_] );}
};
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;
}
int main(void)
{Color3d h={0.78373, 0.1383, 1-0.78373-0.1383};
Image<Color3d> g(768, 1024);
cout << "Inizio\n";
fill(g, h);
cout << "(r,g,b)==(" << g[0][0].r << ", "
<< g[0][0].g << ", "
<< g[768][1024].b << ")\n";
cout << "end\n";
return 0;
}