使用 QGenericMatrix 作为另一个类的属性
Using a QGenericMatrix as an attribute for another class
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;
相关文章:
- 如何使用指针从另一个类访问属性
- 让 Redhawk 组件访问另一个组件的属性
- 使用 QGenericMatrix 作为另一个类的属性
- C++:对于继承多个类的类,如何从一个类继承某些属性,从另一个类继承其他属性?
- 类实例的C++/向量作为另一个类的属性
- LNK2019与另一个工作项目具有相同属性的项目上的错误
- 具有另一个类对象的属性的类构造函数
- 只有Arduino的第一个对象在包含在另一个对象的集合/数组中时会丢失其数据属性值
- 如何有效地从该对象中包含的另一个对象访问对象字段/属性
- C++:修改一个类在另一个类中的私有属性
- 对象作为另一个对象c++的属性
- 如何在C++中更改另一个类函数中的对象属性?
- 从另一个函数更改窗体"visible"属性?
- 在具有C++中另一个类类型的类中声明属性
- 我们可以为一个类指定一个属性名称吗?即使该属性是另一个表中的列
- BGL:使用绑定属性来存储另一个顶点的顶点描述符
- 包含"random"值的类的数组属性(通过另一个类的指针访问时)
- 从另一个类C++中获取对象的属性
- 如何创建一个类来保存另一个类的对象数组,如何操作其他类的私有属性
- 作为属性指向另一个类的指针