来自Java,PHP背景,我试图进入C++。我想在一个结构中存储一个数组。我的问题是在初始化结构之后指定数组的大小。结构中的数组,指针[C++初学者]
这里是我的结构代码:
struct SpriteAnimation {
// ...
int parts; // total number of animation-parts
unsigned int textures[]; // array to store all animation-parts
// ...
};
这里的主要功能:
SpriteAnimation bg_anim;
bg_anim.parts = 3;
unsigned int *myarray = new unsigned int[bg_anim.parts];
bg_anim.textures = myarray;
什么我需要改变,以解决这一问题?
然后将它与'bg_anim.textures = new unsigned int [bg_anim.parts];'一起使用是否正确? – Ben
当你复制你的结构时会发生什么?发生异常时会发生什么? –
@Ben yes工作正常,事实上它之前不正确,因为texture是一个动态分配的数组,应该显示为一个指针。如果您希望使用C++,则应该查找类,因为您使用的是c样式语法。还要记得在结尾 – Will03uk