Qt 有一个名为QGenericMatrix
的类。它的定义如下:
template <int N, int M, typename T>
class QGenericMatrix
{...}
我想在我自己的 MyClass
类中将它用作属性 colorMatrix
,然后像这样在其构造函数中初始化它:
MyClass::MyClass(int n, int m)
{
colorMatrix = new QGenericMatrix<n, m, QColor>;
}
但是语法可能非常不正确。头文件和构造函数中的声明应该怎么写?
最佳答案
由于模板参数必须在编译时已知,您有两个选择:
修复 MyClass 中的矩阵大小和类型。例如
class MyClass { QGenericMatrix<2, 3, QColor> colorMatrix; }
将 MyClass 定义为采用与 QGenericMatrix 相同的模板参数的模板,并使用 MyClass 的模板参数实例化 colorMatrix。像这样:
template<int N, int M, typename T> class MyClass { MyClass() { // init } T entry(int i, int j); QGenericMatrix<N, M, T> colorMatrix; } template<int N, int M, typename T> T MyClass::entry(int i, int j) { return colorMatrix(i, j); }
当然,在这种情况下,MyClass 必须使用模板参数进行实例化,这些模板参数必须在编译时再次已知。
MyClass<2, 3, QColor> myClass;
关于c++ - 使用 QGenericMatrix 作为另一个类的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50375002/