How to define a static array without contant size in class constructor? (C ++)

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:

int presc[width][height];

however, I cannot define it inside the class, so it can create a pointer to a 2D array (and, out of curiosity, 3, 4 and 5D arrays), have it as a member of the class and initialize it in the constructor, for example:

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

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

EDIT: , presc , :

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]
+3
3

"" " ", , Obj. OTOH, w h :

template <int W, int H>
class Obj {
public:
    // ...
private:
    int presc[W][H];
}
+2

. .

, ( , std::vector).

+4

boost::arrayand std::tr1::arrayboth provide constant size arrays. Understand that they are creating completely new types; not using a dynamic array is likely to make your code harder to write than you need. You will have to parameterize your class, as well as any functions that work with these objects. And all that you will save is heap allocation.

0
source

Source: https://habr.com/ru/post/1739653/


All Articles