2017-12-07 8 views
0

일반 매트릭스 곱셈을 수행하는 연산자 *를 이미 구현 한 일반 Matrix 클래스가 있다고 가정합니다.파생 클래스에 대한 내부 연산자 구현

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

어떻게 downcasting 일 것? – Dooggy

+0

좋은 점은, Matrix3 클래스를 Matrix 클래스에서 구성 할 수있게 해준 것이라면, IDE atm에 액세스하지 않고 코드를 작성했기 때문에 코드를 약간 조정할 것입니다. – TheMPC