Another thing I have yet to encounter with the above code, while I get all the template calls, is the constructor, I have seen this used before but I'm unsure what the syntax refers to. I have also tried to look it up the tutorials on this site don't seem to use it at any time, and I can't find reference in my book:
1 2
Array2D(unsigned wd,unsigned ht)
: nWd(wd), nHt(ht), pAr(0) // <---- what does the syntax mean?
If I had to guess to me they just seem like variables of the types. Only this is a funkier way to declare them.e.g. instead of saying:: nWd = wd; with unsignedint nWd; down in the privates.
I would like to know if this is template only syntax or can be used in my class.
Also the math in the operator() overloading, why [ y*width+x ]; to me this seems like creating an object of this class would then go create an array[ width*width + height ];
I have seen this used before but I'm unsure what the syntax refers to
It's the initializer list.
It lets you call specific constructors of your parent class and/or data members (which you can't do inside the body of the ctor).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
class A
{
public:
A(int foo,int bar); // say we want to call this ctor
};
class B
{
private:
A a; // we can't call it here
public:
B() : a(1,2) // so we call it here
{
// We also can't call it here
}
};
The members should be listed in the same order they are listed in the class. So because nWd is declared first in the Array2D class definition, that's why it's first in the initializer list.
I would like to know if this is template only syntax or can be used in my class.
Any class.
Also the math in the operator() overloading, why [ y*width+x ]; to me this seems like creating an object of this class would then go create an array[ width*width + height ];
The array is stored in "read like a book" order. Top row left to right, then the next row, left to right, etc. If you have a 2x3 array, it's stored linearly like this:
0 1 2 3 4 5
But conceptually it's stored like this:
1 2
0 1 2
3 4 5
y*width + x works to get the appropriate index. Since each row has 'width' elements, to get the start of row Y, you need to multiply Y by width.