1

私は C++ のクラスが初めてで、ファイルからデータを読み取り、3D グリッドを作成するメソッドを持つクラス "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

今、私は自分の「プロット」オブジェクトを作成し、その後、「グリッド」配列を動的に作成したいと考えています。これを行うことは可能ですか?または、最初に「プロット」を作成するときに、配列「グリッド」のサイズを宣言する必要がありますか?

4

4 に答える 4

4

std::vector grid;会員としてご利用ください。次に、使用grid.resize(nx*ny*nz)して必要なサイズを強制するgrid.push_back(value);、配列に追加する各値に使用できます。

于 2011-09-27T14:59:28.440 に答える
2

可能です:

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);

編集:

ただし、マークBの答えを検討する必要があります。std::また、動的に割り当てられた配列ではなく、から何かを使用します。管理がはるかに簡単です。

EDIT2:

コンスタンチニウスの答えによると、init()可能な場合はメソッドを避けてください。構築後の初期化が特に必要な場合はそれを使用します。それ以外の場合は、すべての初期化ロジックをコンストラクターに保持します。

于 2011-09-27T15:02:07.050 に答える
1

それはあなたの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;
}
于 2011-09-27T15:02:25.880 に答える
0
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 を使用します。

于 2011-09-27T16:47:01.803 に答える