Set/Getについては()演算子をオーバーロードして要素にアクセスできるようにしましょう.
class Matrix2x2
{
public:
double& operator()(int row, int col);
};
double& Matrix2x2::operator()(int row, int col)
{
return mat[2*row+col];
}
さて,何度かでてきていますが,double&の&は参照と言います.参照というのは別名(エイリアス)とも言い,コピーではなくオリジナルと同一です.
PCでいうとコピーではなく,ショートカットを作るようなものです.
ワードファイルがあり,コピーして,コピーしたものを変更してもオリジナルは変わりませんが,
ショートカットを作って,ショートカットから起動して変更した場合はオリジナルが変更されるのと同じです.
&の無い返り値は
値返しといい数値のコピーが返されます.
単なる数値なので代入,つまりSetはできません.
参照返しの場合,mat[x]を返します.値ではなくその値の場所を返します.
どのように使えるか見てみましょう.
Matrix2x2 a(1.0, 2.0, 3.0, 4.0);
std::cout << a(0,0) << std::endl; // Get(0,0)と同じ
a(0,0) = 2.0; //左辺はa::mat[0]への参照なので,mat[0]に2.0を代入できる.Set(0, 0, 2.0)と同じ
よって,Multiplyのコードを見やすいものに変更することができます.
Matrix2x2 Matrix2x2::Multiple(Matrix2x2 const& other) const
{
Matrix2x2 result;
result(0,0) = this->Get(0,0)*other.Get(0,0)+this->Get(0,1)*other.Get(1,0);
result(0,1) = this->Get(0,0)*other.Get(0,1)+this->Get(0,1)*other.Get(1,1);
result(1,0) = this->Get(1,0)*other.Get(0,0)+this->Get(1,1)*other.Get(1,0);
result(1,1) = this->Get(1,0)*other.Get(0,1)+this->Get(1,1)*other.Get(1,1);
return result;
}
では前回のmainを今回の実装で書き換えてみましょう.
int main()
{
Matrix2x2 mat(1, 2, 3, 4);
Matrix2x2 inv_mat = !mat;
std::cout << "mat = " << std::endl;
mat.PrintMatrix();
std::cout << "inv_mat = " << std::endl;
inv_mat.PrintMatrix();
Matrix2x2 mul_mat = mat * inv_mat;
std::cout << "mul_mat = " << std::endl;
mul_mat.PrintMatrix();
// 追加.一気に逆行列と掛け算ができます
Matrix2x2 mul_mat2 = mat * !mat;
std::cout << "mul_mat2 = " << std::endl;
mul_mat2.PrintMatrix();
::system("PAUSE");
return 0;