1
は、私は、過負荷をオペレーターに新しいです となりましオペレータ[] []オーバーロードを使用して 行列を初期化しようとしています。演算子のオーバーロード行列C++ **プロキシなし**
お困り 行列を次のように初期化したいと考えています: 行列[0] [0] = 100
[] []を使うことはできますか?どのように私はそれらを使用するのですか?
//main.cpp//
void main()
{
Matrix m(2, 2);
m[0][0] = 2;
}
//matrix.h//
#ifndef MATRIX_H
#define MATRIX_H
class Matrix
{
public :
Matrix(int ,int);
Matrix operator [][] // *****im stuck here*****
private :
int numRows;
int numCols;
int **matrix;
}
#endif
//matrix.cpp//
#include <iostream>
#include "Matrix.h"
using namespace std;
Matrix::Matrix(int rows,int cols)
:numOfCols(cols),numOfRows (rows)
{
matrix=new int *[cols];
for(int i=0;i<rows;i++)
{
matrix[i]=new int [cols];
for(int c=0;c<cols;c++)
{
matrix[i][c]=0;
}
}
}
このコードをすべて書き込むのではなく、単に 'std :: array、rowCount> '?それでも 'foo [y] [x]'構文が得られます。 –
@Jackは、値のギャップのない配列が必要な場合、悪い考えをしています。どこか別の場所で使う。パフォーマンスPOVからの悪い考え。それはそれをカットしない教育訓練です。 – Swift