“动态构造函数”在 c++

发布于 2024-12-06 15:35:25 字数 488 浏览 0 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

醉生梦死 2024-12-13 15:35:25

使用 std::vector grid; 作为您的成员。然后您可以使用 grid.resize(nx*ny*nz) 强制设置您想要的大小,或者使用 grid.push_back(value); 对于要添加到数组中的每个值。

Use std::vector grid; as your member. Then you can use grid.resize(nx*ny*nz) to force the size you want or use grid.push_back(value); for each value you want to add to the array.

能否归途做我良人 2024-12-13 15:35:25

这是可能的:

class Plot{
   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
public:
   Plot()
   {
      grid = NULL;
   }
   ~Plot()
   {
       delete[] grid;
   }
   void init(int x, int y, int z)
   {
      delete[] grid; //make sure no memory leaks since grid might have already been allocated
      nx = x;
      ny = y;
      nz = z;
      grid = new double[nx*ny*nz];
   }
};

构造之后,只需调用方法 init:

Plot p();
p.init(2,3,4);

编辑:

但是您应该考虑 Mark B 的答案。我还会使用 std:: 中的内容而不是动态分配的数组。更容易管理。

EDIT2:

根据 Constantinius 的回答,尽可能避免 init() 方法。如果您特别需要在构造后进行初始化,请使用它,否则将所有初始化逻辑保留在构造函数中。

It is possible:

class Plot{
   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
public:
   Plot()
   {
      grid = NULL;
   }
   ~Plot()
   {
       delete[] grid;
   }
   void init(int x, int y, int z)
   {
      delete[] grid; //make sure no memory leaks since grid might have already been allocated
      nx = x;
      ny = y;
      nz = z;
      grid = new double[nx*ny*nz];
   }
};

After the construction, just call the method init:

Plot p();
p.init(2,3,4);

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.

寄居人 2024-12-13 15:35:25

这取决于您的 Plot 类的使用方式。如果您认为有必要仅创建具有有效大小的此类对象,则不应允许使用默认构造函数。您可以通过定义自己的构造函数来完成此操作:

public:
Plot(int _nx, int _ny, int _nz) : nx(_nx), ny(_ny), nz(_nz) 
{
    // initialize your array
    grid = new double[nx*ny*nz];
}

也不要忘记析构函数来清除分配的内存:

~Plot() 
{
    delete[] grid;
}

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:

public:
Plot(int _nx, int _ny, int _nz) : nx(_nx), ny(_ny), nz(_nz) 
{
    // initialize your array
    grid = new double[nx*ny*nz];
}

also don't forget your destructor to clear the allocated memory:

~Plot() 
{
    delete[] grid;
}
绝對不後悔。 2024-12-13 15:35:25
class Plot {
    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
public:
    /* default constructor */
    Plot() : nx(0), ny(0), nz(0), grid(NULL) { }
    /* rule of five copy constructor */
    Plot(const Plot& b) : nx(b.nx), ny(b.ny), nz(b.nz) {
        int max = nx*ny*nz;
        if (max) {
            grid = new double(max);
            for(int i=0; i<max; ++i)
                grid[i] = b.grid[i];
        } else
            grid = NULL;
    }
    /* rule of five move constructor */
    Plot(Plot&& b) : nx(b.nx), ny(b.ny), nz(b.nz) grid(b.grid) { b.grid = NULL; }
    /* load constructor */
    Plot(std::istream& b) : nx(0), ny(0), nz(0), grid(NULL) { Load(b); }
    /* rule of five destructor */
    ~Plot() { delete[] grid; }
    /* rule of five assignment operator */
    Plot& operator=(const Plot& b) {
        int max = b.nx*b.ny*b.nz;       
        double* t = new double[max];
        for(int i=0; i<max; ++i)
            t[i] = b.grid[i];            
        //all exceptions above this line, NOW we can alter members
        nx = b.nx;
        ny = b.ny;
        nz = b.nz;
        delete [] grid;
        grid = t;
    }
    /* rule of five move operator */
    Plot& operator=(Plot&& b) {   
        nx = b.nx;
        ny = b.ny;
        nz = b.nz;
        delete [] grid;
        grid = b.grid;
        b.grid = NULL;
    }
    /* always a good idea for rule of five objects */
    void swap(const Plot& b) {
        std::swap(nx, b.nx);
        std::swap(ny, b.ny);
        std::swap(nz, b.nz);
        std::swap(grid, b.grid);
    }

    /* your load member */
    void Load(std::istream& in) {
        //load data
        //all exceptions above this line, NOW we can alter members
        //alter members
    };
};

int main() {
     Plot x;  //default constructor allocates no memory
     Plot y(x); //allocates no memory, because x has no memory
     Plot z(std::cin); //loads from stream, allocates memory
     x = z; //x.grid is _now_ given a value besides NULL.
}

我想这回答了你的问题。仍然:使用 std::vector。

class Plot {
    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
public:
    /* default constructor */
    Plot() : nx(0), ny(0), nz(0), grid(NULL) { }
    /* rule of five copy constructor */
    Plot(const Plot& b) : nx(b.nx), ny(b.ny), nz(b.nz) {
        int max = nx*ny*nz;
        if (max) {
            grid = new double(max);
            for(int i=0; i<max; ++i)
                grid[i] = b.grid[i];
        } else
            grid = NULL;
    }
    /* rule of five move constructor */
    Plot(Plot&& b) : nx(b.nx), ny(b.ny), nz(b.nz) grid(b.grid) { b.grid = NULL; }
    /* load constructor */
    Plot(std::istream& b) : nx(0), ny(0), nz(0), grid(NULL) { Load(b); }
    /* rule of five destructor */
    ~Plot() { delete[] grid; }
    /* rule of five assignment operator */
    Plot& operator=(const Plot& b) {
        int max = b.nx*b.ny*b.nz;       
        double* t = new double[max];
        for(int i=0; i<max; ++i)
            t[i] = b.grid[i];            
        //all exceptions above this line, NOW we can alter members
        nx = b.nx;
        ny = b.ny;
        nz = b.nz;
        delete [] grid;
        grid = t;
    }
    /* rule of five move operator */
    Plot& operator=(Plot&& b) {   
        nx = b.nx;
        ny = b.ny;
        nz = b.nz;
        delete [] grid;
        grid = b.grid;
        b.grid = NULL;
    }
    /* always a good idea for rule of five objects */
    void swap(const Plot& b) {
        std::swap(nx, b.nx);
        std::swap(ny, b.ny);
        std::swap(nz, b.nz);
        std::swap(grid, b.grid);
    }

    /* your load member */
    void Load(std::istream& in) {
        //load data
        //all exceptions above this line, NOW we can alter members
        //alter members
    };
};

int main() {
     Plot x;  //default constructor allocates no memory
     Plot y(x); //allocates no memory, because x has no memory
     Plot z(std::cin); //loads from stream, allocates memory
     x = z; //x.grid is _now_ given a value besides NULL.
}

This answers your questions I think. Still: use a std::vector.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文