“动态构造函数”在 c++
我是 C++ 类的新手,我需要创建一个“Plot”类,它有一个从文件中读取数据并创建 3d 网格的方法。
我知道您可以使用默认值创建“默认”构造函数,也可以使用预定义值创建特殊的构造函数。
在我的“私人”部分,我有:
int nx; // number of "x" values
int ny; // number of "y" values
int nz; // number of "z" values
double* grid; // one dimensional array which stores nx*ny*nz values
double tgrid(int ix, int iy, int iz); // function which searches grid and returns value
现在,我想创建我的“绘图”对象,然后动态创建“网格”数组。可以这样做吗?或者当我第一次创建“plot”时我需要声明数组“grid”的大小吗?
i am new to classes in C++ and i need to create a class "Plot" which has a method that reads in data from a file and creates a 3d grid.
i understand that you can make "default" constructors with default values or you can create a special constructor with predefined values.
in my "private" section, i have:
int nx; // number of "x" values
int ny; // number of "y" values
int nz; // number of "z" values
double* grid; // one dimensional array which stores nx*ny*nz values
double tgrid(int ix, int iy, int iz); // function which searches grid and returns value
now, i want to create my "plot" object and then AFTER that, dynamically create the "grid" array. is it possible to do this? or will i need to declare the size of the array "grid" when i first create "plot"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用
std::vector grid;
作为您的成员。然后您可以使用 grid.resize(nx*ny*nz) 强制设置您想要的大小,或者使用 grid.push_back(value); 对于要添加到数组中的每个值。Use
std::vector grid;
as your member. Then you can usegrid.resize(nx*ny*nz)
to force the size you want or usegrid.push_back(value);
for each value you want to add to the array.这是可能的:
构造之后,只需调用方法 init:
编辑:
但是您应该考虑 Mark B 的答案。我还会使用 std:: 中的内容而不是动态分配的数组。更容易管理。
EDIT2:
根据 Constantinius 的回答,尽可能避免
init()
方法。如果您特别需要在构造后进行初始化,请使用它,否则将所有初始化逻辑保留在构造函数中。It is possible:
After the construction, just call the method init:
EDIT:
You should however consider Mark B's answer. I would also use something from
std::
rather than a dynamically allocated array. Much easier to manage.EDIT2:
As per Constantinius' answer, avoid
init()
methods when you can. If you specifically need the initalization AFTER construction, use it, otherwise keep all initialization logic in the constructor.这取决于您的
Plot
类的使用方式。如果您认为有必要仅创建具有有效大小的此类对象,则不应允许使用默认构造函数。您可以通过定义自己的构造函数来完成此操作:也不要忘记析构函数来清除分配的内存:
It depends on how your
Plot
class should be used. If you consider it necessary to create objects of this class only with valid sizes you should not allow a default constructor. You do this by defining your own constructor like that:also don't forget your destructor to clear the allocated memory:
我想这回答了你的问题。仍然:使用 std::vector。
This answers your questions I think. Still: use a std::vector.