2017-12-07 8 views
0

通常の行列乗算を実行する演算子*を既に実装している一般的な行列クラスがあるとします。派生クラスの内部演算子を実装する

Matrix operator*(const Matrix &) const; 

私は今、3×3行列を表す継承クラスMatrix3のための別の*演算子を実装したい:

、オペレータは、次のシグネチャを有しています。

それは次のシグネチャだろう:(

Matrix3 operator*(const Matrix3 &) const; 

私はすでに、基本クラスのために書かれたコードを再利用するために、この演算子を実装するための適切な方法を探していますが、コストを最小限に抑えるために、すなわちコピー)。

答えて

1

これはうまく動作するはずです:

// Either return base class 
Matrix operator*(const Matrix3& other) const 
{ 
    return Matrix::operator*(other); 
} 

// Or construct from a Matrix 
Matrix3 operator*(const Matrix3& other) const 
{ 
    return Matrix3(Matrix::operator*(other)); 
} 

// Either construct the Matrix data in the Matrix3 
Matrix3(const Matrix& other) 
{ 
    // Initialize Matrix specifics 
    // Initialize Matrix3 specifics 
} 

// Or pass the Matrix to it's base class so it can take care of the copy 
Matrix3(const Matrix& other) : Matrix(other) 
{ 
    // Initialize Matrix3 specifics 
} 
+0

どのようにダウンキャストの仕事でしょうか? – Dooggy

+0

良い点、私がMatrix3クラスをMatrixクラスから構築できるようにするには、IDE atmへのアクセス権を持たずに書いたので、コードを少し調整します。 – TheMPC

関連する問題