Create a UserArray of bit fields which can be declared as follows: The size occupied by our Array will be less then a normal array. Suppose we want an ARRAY of 20 FLAGs (TRUE/FALSE). A bool FLAG[20] will take 20 bytes of memory, while UserArray<bool,bool,0,20> will take 4 bytes of memory.
- Use class Template to create user array.
- Use Bit wise operators to pack the array.
-
Equality operation should also be implemented.
template<class T,int W,int L,int H>//i have used template<class T> //but never used such way class UserArray{ //.... }; typedef UserArray<bool,4,0,20> MyType;
where:
- T = type of an array element
- W = width of an array element, 0 < W < 8
- L = low bound of array index (preferably zero)
- H = high bound of array index
A main program:
int main() {
MyType Display; //typedef UserArray<T,W,L,H> MyType; defined above
Display[0] = FALSE; //need to understand that how can we write this?
Display[1] = TRUE; //need to understand that how can we write this?
//assert(Display[0]);//commented once, need to understand above code first
//assert(Display[1]);//commented once..
//cout << “Size of the Display” << sizeof(Display);//commented once..
}
My doubt is how those parameters i.e T,L,W & H are used in class UserArray and how can we write instance of UserArray as Display[0] & Display[1] what does it represent?
Short & simple example of similar type will be easy for me to understand.
W,LandHare non-type template parameters. You can instantiate a template (at compile-time) with constant values, e.g.:In the example above, everywhere that
Nappears in the template definition, it will be replaced at compile-time by the supplied constant.Note that
MyArray<7>andMyArray<8>are completely different types as far as the compile is concerned.I have no idea what the solution to your specific problem is. But your code won’t compile, currently, as you have not provided values for the template parameters.