我有两个类来定义一些操作,并记录矩阵行和列。一个用于主机,另一个用于设备。
struct device_matrix {
device_vector<double> data;
int row;
int col;
// constructors
device_matrix() ...
// some matrix operations
add()...
}
struct host_matrix {
host_vector<double> data;
int row;
int col;
// constructors
host_matrix() ...
// some matrix operations
add()...
}
基类应该看起来像:
template<typename T>
struct base_matrix {
T data;
int row;
int col;
// no constructors
// some matrix operations
add()...
}
但除了数据类型和构造函数之外,其他人员都是一样的。我必须实现三个目标,一个是将类型T专门化为device_vector或host_vector,另一个是为这两个结构编写不同的构造函数,并继承操作方法。我怎么能同时做到这一点?
谢谢!
怎么样
template<template<typename T> class V>
struct base_matrix
{
V<double> data;
...
virtual void add(...) = 0;
};
struct host_matrix : base_matrix<host_vector>
{
host_matrix() { ... }
...
void add(...) { ... }
};
如果你不能使用模板模板,那么如下:
template<typename V>
struct base_matrix
{
V data;
...
virtual void add(...) = 0;
};
struct host_matrix : base_matrix<host_vector<double>>
{
host_matrix() { ... }
...
void add(...) { ... }
};