私はMatrix
クラスを持っていて、それは*
スカラーと行列の乗算のためのオーバーロードされた演算子を持っています。
template <class T> class Matrix
{
public:
// ...
Matrix operator*(T scalar) const;
// ...
}
// ...
template <class T>
Matrix<T> Matrix<T>::operator*(T RightScalar) const
{
Matrix<T> ResultMatrix(m_unRowSize, m_unColSize);
for (uint64_t i=0; i<m_unRowSize; i++)
{
for (uint64_t j=0; j<m_unColSize; j++)
{
ResultMatrix(i, j) = TheMatrix[m_unColSize * i + j] * RightScalar;
}
}
return ResultMatrix;
}
// ...
行列オブジェクトに右側からスカラーを問題なく乗算できます。
Matrix<double> X(3, 3, /* ... */); // Define a 3x3 matrix and initialize its contents
Matrix<double> Y; // Define an output matrix
Y = X * 10.0; // Do the linear operation
しかし、どうすれば左側から同じように掛けることができますか?
Matrix<double> X(3, 3, /* ... */);
Matrix<double> Y;
Y = 10.0 * X;
算術では、乗算を行うときに左側に定数を書き込むのが一般的な表記法です。コードを読みやすくするために、このルールに従いたいと思います。
これをC++で実装することは可能ですか?
可能であれば、コードのクラスメソッドを変更するにはどうすればよいですか?