views:

144

answers:

3

I have a class defined as:

class Obj {
public:
    int width, height;
    Obj(int w, int h);
}

and I need it to contain a static array like so:

int presc[width][height];

however, I cannot define within the class, so it it possible to create a pointer to a 2D array (and, out of curiosity, 3, 4, and 5D arrays), have that as a member of the class, and intitalize it in the constructor like:

int ar[5][6];
Obj o(5, 6, &ar);

EDIT: The idea here is that every object will have a different width and height, so the array I use to represent that object will be unique to the object, but once that array is defined (preferably in the constructor), it will not change. And the values of width and height for a particular object are known at compile time.

EDIT: The arrays are for collision detection by superimposing the presc arrays of two objects onto one large array, and seeing where the overlap, declarations like so:

Obj player1(32, 32); //player with a width of 32 px and height of 32 px, presc[32][32]
Obj boss(500, 500); //boss with a width of 500 px and height of 500 px, presc[500][500]
+2  A: 

If, by "dynamic", you mean "heap-allocated", then no, there is no way to this with the current Obj. OTOH, if you know w and h at compile time:

template <int W, int H>
class Obj {
public:
    // ...
private:
    int presc[W][H];
}
Marcelo Cantos
That's the same as the original code, just dressed up in templates. If he knew w and h at compile time, his original code could use whatever you'd substituting for the template parameters.
Ben
Actually w/width and h/height are known at compile time (apologies if I wasn't clear at first). The idea is that I'll know the h and w values for a particular object at compile time, they just won't be the same for every object, and so I have to use variables for the size.
Keand64
@Ben, what is wrong with suggesting a correct answer that didn't occur to the OP?
Marcelo Cantos
I wrote the response before the OP updated the question to make it clear that he needed multiple versions of the class with different h and w. Using a template class is, indeed, the best solution.
Ben
Thanks for agreeing, @Ben. But just to be clear, I also wrote my response before the OP clarified the requirement. That's why I prefaced my sample code with, "if you know `h` and `w` at compile time".
Marcelo Cantos
+4  A: 

No. The size of the class needs to be known at compile time.

If you don't know the size of the array until run time, you can't have that array as a class member (you'll need to dynamically allocate the array and store a pointer to it in the class or, preferably, use a std::vector).

James McNellis
I do know the size of the array at compile time, it just won't be the same for every object, so I can't use a constant variable.
Keand64
@Keand64: In that case, you have the option of using a template to set the width and height at compile-time, like Marcelo demonstrates in his answer. The cost of that is that objects with different array dimensions have different types. You may be able to get around this by using inheritance, but it can get messy.
James McNellis
A: 

boost::array and std::tr1::array both provide constant-size arrays. Understand that these create whole new types; not using a dynamic array will probably make a lot of your code harder to write than it needs to be. You'll have to parameterize your class, as well as any functions that work on these objects. And all you'll be saving is a single heap allocation.

Dennis Zickefoose