English 中文(简体)
采用C++模板分类的简单矩阵例子
原标题:Simple matrix example using C++ template class

我正试图用C++模板撰写一个三维矩阵级,试图粉碎我的C++,并向一位编码员解释一些内容。

这是我迄今为止所做的:

template class<T>
class Matrix
{
public:
    Matrix(const unsigned int rows, const unsigned int cols);
    Matrix(const Matrix& m);
    Matrix& operator=(const Matrix& m);
    ~Matrix();

    unsigned int getNumRows() const;
    unsigned int getNumCols() const;

    template <class T> T getCellValue(unsigned int row, unsigned col) const;
    template <class T> void setCellValue(unsigned int row, unsigned col, T value) const;

private:
    // Note: intentionally NOT using smart pointers here ...
    T * m_values;
};


template<class T> inline T Matrix::getCellValue(unsigned int row, unsigned col) const
{
}

template<class T> inline void Matrix::setCellValue(unsigned int row, unsigned col, T value)
{
}

我 st在上,因为我需要分配新的东西。 T,它似乎需要一种模板方法,但我不敢肯定,我以前曾过过一个模版的导师。

我怎么能够 imp击ctor者?

最佳回答

您可在施工人查阅<代码>T,因此施工人本身不必成为模板。 例如:

Matrix::Matrix(const unsigned int rows, const unsigned int cols)
{
    m_values = new T[rows * columns];
}

考虑使用智能标识,如<代码>boost: 镜子-array或std:vector,使资源管理更为容易。

如果您的矩阵有固定规模,另一种选择是把浏览量和栏目作为模板参数与T一道:

template <class T, unsigned Rows, unsigned Columns>
class Matrix 
{ 
    T m_values[Rows * Columns];
};

最大的优势是,规模是矩阵的一部分,可以用来在汇编时执行规则,例如,确保两个矩阵在进行矩阵重复时具有相容的规模。 这还不要求有活力地分配这些阵列,使资源管理更加容易。

最大的不利之处是,你可以改变矩阵的规模,因此不能满足你们的需要。

问题回答

暂无回答




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签