配列の配列の変数を持つクラスがあります。
private double * array[4][4];
この方法で初期化できますか?
void RealMatrix::init(int i, int j) {
this->array[i][j] = new double;
*(this->array[i][j]) = 0;
}
配列の配列の変数を持つクラスがあります。
private double * array[4][4];
この方法で初期化できますか?
void RealMatrix::init(int i, int j) {
this->array[i][j] = new double;
*(this->array[i][j]) = 0;
}
はい、できます。ただし、私があなたの立場にある場合は、double
ポインターを取り除きます (このようにする特定の理由がない限り)。
これにより、動的に単一が割り当てられ、2D 配列のdouble
要素であるポインターがその .xml を指すように設定されます。次に、それに値 0 を割り当てます。これは、次のように 1 行で実行できます。[i][j]
double
double
this->array[i][j] = new double(); // value-initialization
または、次のように明示的に設定することもできます0
。
this->array[i][j] = new double(0); // direct-initialization
double
ただし、よほどの理由がない限り、配列メンバーにs ではなく sdouble*
を含めたほうがよいでしょう。
double array[4][4];
必要がない限り、何かを動的に割り当てる意味はありません。配列が非常に小さいという正当な理由があるとは想像できません。
を使用しvector<double>
ます。
class Matrix {
std::vector<double> values;
const unsigned int width;
public:
Matrix(unsigned int width, unsigned int height)
: values(width * height), width(width) {
}
double& value(unsigned int i, unsigned int j) {
return values[i + (width * j))];
}
};
使用法:
Matrix matrix(10, 20);
matrix.value(3, 4) = 15;