2017-03-02 130 views
0

我定义如下模板类:完全专注与模板的构造函数模板类

template<unsigned int N, unsigned int L> 
class Matrix 
{ 
    public: 

     Matrix(const float k = 0.0f) 
     { 
      for(unsigned int i = 0; i < N; ++i) 
       for(unsigned int j = 0; j < L; ++j) 
       { 
        if(i == j) m[i][j] = k; 
        else m[i][j] = 0.0f; 
       } 
     } 

     //With other constuctors, methods and operators 

    protected: 

     float m[N][L]; 
}; 

我想为矩阵< 4U一个专业化,4U>
所有的功能都将做同样的事情从模板中除一人外的那些,我将覆盖:

Vec3 operator*(const Vec3& vec) 
{ 
    Vec3 v; 

    if(L != 3) 
    { 
     if(Core::Debug::Log::CoreLogIsActive) 
      Core::Debug::Log::ConsoleLog("Number of column of the matrix is different from the number of column of the vector", 
       Core::Debug::LogLevel::Error); 
     return v; 
    } 

    v.x = m[0][0] * vec[0] + m[0][1] * vec[1] + m[0][2] * vec[2]; 
    v.y = m[1][0] * vec[0] + m[1][1] * vec[1] + m[1][2] * vec[2]; 
    v.z = m[2][0] * vec[0] + m[2][1] * vec[1] + m[2][2] * vec[2]; 

    return v; 
} 

(它会做不同的东西)

我应该如何进行specilization?
其他功能不会改变我的specilized矩阵< 4u,4u>但是如果我做了一个类specilization我必须specilize每个成员不会我?

我的猜测是对specilize只需要成员函数,那么就使用

using Matrix4 = Matrix<4u, 4u> 

这样够了?
我将能够在特化使用操作员从Matrix < 4U,4U>这样的:

template <> 
Vec3 Matrix<4u, 4u>::operator*(const Vec3& vec) 
{ 
    Vec4 vec4d(vec); 

    vec4d = Matrix<4u, 4u>::operator*(vec); 

    return Vec3(vec4d.x, vec4d.y, vec4d.z); 
} 

另外,我打算再使用继承与Matrix4

感谢您的时间

回答

0

嗯,我说的话似乎工作(我有错别字,我找不到......)

解决方案:

template <> 
Vec3 Matrix<4u, 4u>::operator*(const Vec3& vec) 
{ 
    Vec4 vec4d(vec); 

    vec4d = Matrix<4u, 4u>::operator*(vec); 

    return Vec3(vec4d.x, vec4d.y, vec4d.z); 
} 

using Matrix4 = Matrix<4u, 4u>