Re: Class array ? (2 dimensional)
eb wrote:
Alan Johnson wrote:
You'll save yourself a lot of headache if you use std::vector. Example:
unsigned rows = 25, cols = 80 ;
std::vector< std::vector<T> > v(rows, std::vector<T>(cols)) ;
Well, it's not that simple to me ...
I'm using a QT class as T : QCanvasLine
I started with a simple version :
unsigned rows = 25
std::vector< QCanvasLine > v(rows) ;
There are two different forms of vector's constructor that are used in
the line of code I proposed. The first is:
vector<T> v(num) ;
This makes a vector containing num objects of type T, and uses T's
default constructor to initialize them. An alternative is:
vector<T> v(num, object) ;
This creates a vector with num copies of object. Looking at the
documentation for QCanvasLine (disclaimer: I don't know Qt), it doesn't
have a default constructor, so the first form won't work. However,
something like the following should (assuming canvas has been properly
defined):
size_t cols = 80 ;
std::vector<QCanvasLine> vcols(cols, QCanvasLine(canvas)) ;
That will make 80 copies of the temporary object created by
QCanvasLine(canvas).
Now, you want two dimensions, so what you want is several copies of the
vector we just defined:
size_t rows = 25 ;
std::vector< std::vector<QCanvasLine> > lines(rows, vcols) ;
Or, if you want to do it all together:
size_t rows = 25, cols = 80 ;
std::vector< std::vector<QCanvasLine> > lines(rows,
std::vector<QCanvasLine>(cols, QCanvasLine(canvas))) ;
--
Alan Johnson