Я пишу матричный класс (CMatrix) с такими производными классами, как трехмерный вектор (CVector) и матрица вращения (CRotMatrix).Мой объект CMatrix может быть умножен на другой объект на основе CMatrix или на любое числовое значение (скалярное).Этот код представляет суть проблемы, которую я получил:
template<class T> class CMatrix
{
public:
template<class U> const CMatrix& operator=(const CMatrix<U> &inp){return (*this);}
CMatrix& operator*(const CMatrix &inp)
{
cout<<"Multiplication by CMatrix"<<endl;
return (*this);
}
template<class U>
CMatrix& operator*(const U &inp)
{
cout<<"Multiplication by a scalar."<<endl;
return (*this);
}
};
template<class T> class CVector: public CMatrix<T>{};
template<class T> class CRotMatrix: public CMatrix<T>{};
int main()
{
CMatrix<int> foo1;
CMatrix<int> foo2;
CVector<int> dfoo1;
CRotMatrix<int> dfoo2;
foo1 = foo1*foo2; //calls CMatrix method
foo1 = foo1*5; //calls scalar method
foo1 = foo1*dfoo2; //calls scalar method, shoud be CMatrix
foo1 = dfoo2*dfoo1; //calss scalar method, shoud be CMatrix
return 0;
}
Проблема в том, что компилятор предпочитает шаблонную версию оператора * ().Есть ли способ заставить компилятор выбрать правильный метод для производных классов CMatrix в этой ситуации?Если я отключу этот метод
CMatrix& operator*(const U &inp)
Компилятор делает это правильно, но класс теряет способность умножаться на скаляр.Я использую msvc10.Заранее спасибо.