如何在类的构造函数中定义没有固定大小的静态数组? (C++)
我有一个类定义为:
class Obj {
public:
int width, height;
Obj(int w, int h);
}
并且我需要它包含一个静态数组,如下所示:
int presc[width][height];
但是,我无法在类中定义,因此可以创建一个指向 2D 数组的指针(出于好奇,3, 4和 5D 数组),将其作为类的成员,并在构造函数中将其初始化,如下所示:
int ar[5][6];
Obj o(5, 6, &ar);
编辑:这里的想法是每个对象都有不同的宽度和高度,因此我用来表示该对象的数组将对于对象来说是唯一的,但是一旦定义了该数组(最好在构造函数中),它就不会改变。并且特定对象的宽度和高度值在编译时是已知的。
编辑:这些数组用于通过将两个对象的 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]
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]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不可以。需要在编译时知道类的大小。
如果您在运行时之前不知道数组的大小,则不能将该数组作为类成员(您需要动态分配数组并在类中存储指向它的指针,或者最好使用一个 std::vector )。
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
).如果“动态”是指“堆分配”,那么不,当前的 Obj 无法做到这一点。 OTOH,如果您在编译时知道
w
和h
:If, by "dynamic", you mean "heap-allocated", then no, there is no way to this with the current Obj. OTOH, if you know
w
andh
at compile time:boost::数组
和std::tr1::array
都提供恒定大小的数组。了解这些创造了全新的类型;不使用动态数组可能会使您的许多代码比实际需要的更难编写。您必须参数化您的类以及对这些对象起作用的任何函数。您将节省的只是单个堆分配。boost::array
andstd::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.这个问题及其早期的答案来自2010年。当时它们是正确的,但今天(2023年),它们看起来已经过时了:
从C++11开始,就有 std::array - 一个模板容器,在大多数情况下可用作 C 样式数组的替代品。它与 Marcelo Cantos 的答案提出的模板类似。
像 Autosar C++ 这样的流行编码标准甚至不鼓励继续使用 C 风格的数组:
This question and its earlier answers are from 2010. They were correct at that time, but today (2023), they look outdated:
Since C++11, there is std::array - a template container that is useful as a replacement for C-style arrays in most situations. It does similar things as the template proposed by the answer of Marcelo Cantos
Popular coding standards like Autosar C++ even discourage to continue with C-style arrays: