结构的初始化
Initialization of the structure
在我的程序中我有结构:
struct point {
float x;
float y;
};
编辑:我需要创建
struct Path{
Point array[];
}
用函数init_path(Path *p, int size)
初始化它。
我的问题是,如何定义功能?预先感谢。
您的Path
可能是:
struct Path {
point* points;
};
void init_path(Path *path, int size) {
path->points = new point[size]();
}
但是,为什么您的教授想要一个功能而不是适当的构造函数/破坏者仍然是一个谜。在这里,您仍然需要在某个地方的points
上调用delete[]
。使用以下结构,您不需要任何init
功能,并且对象将正确删除其资源。
struct Path {
Path(unsigned size) : points{ new point[size] } {}
~Path() { delete[] points; }
point* points;
};
相关文章:
- 在C和C++中初始化结构中的数组
- 我们可以用参数化构造函数初始化结构的数组吗?
- 尝试初始化结构内的联合时出错
- 使用其他成员初始化结构的成员?
- 使用构造函数初始化结构还是在之后设置其值更好?
- 如果不初始化结构中的向量,它会自动为空还是具有随机内存位置的值?
- 如何在另一个结构中初始化结构数组?
- 如何在构造函数中初始化结构体的动态数组?
- 初始化结构中的联合 - c++
- 尝试初始化结构向量时出现编译错误
- 用于初始化结构的 void 指针强制转换
- 初始化结构的两种方式
- 使用用户定义的构造函数初始化C++结构
- 使用初始值设定项列表初始化结构,对于 std::array 失败
- 在 c++ 中初始化结构的指针
- 无序列图会创建一个零初始化结构吗?
- 如何在C++中初始化结构
- C 用结构指针初始化结构
- 在C 中初始化结构数组
- 使用数组聚合初始化结构数组