我有一个有很多功能,但本质上是一个向量类模板类。我想添加一个函数只是布尔类型。专业模板类成员函数只有一种类型的
#include <vector>
template <typename T>
class reflected{
private:
T*dev_;
T*host_;
ar_size size;
reflected<T>& operator=(reflected<T>& rhs);//do not impliment. assign not allowed.
reflected(reflected<T>& old); //do not impliment. Copy not allowed.
public:
reflected():size(0L),dev_(NULL),host_(NULL){}
reflected(ar_size n):size(n),dev_(NULL),host_(NULL){init();}
reflected(const T*x,ar_size n):size(n),dev_(NULL),host_(NULL){init();set(x);}
~reflected();
void init();
void init(ar_size n);
void init(const T*x,ar_size n);
void set(const T * x);
void setat(ar_index i, T x);
const T getat(ar_size i);
const T * devPtr();
const T operator [](const ar_index i);
ar_size length(){return size;}
};
我想一个函数vector<ar_index> reflected<bool>::which()
增加反射类,这是它将使意义的唯一个案的特殊情况。做这个的最好方式是什么。编译器似乎不喜欢添加which()来反射,只为bool定义它。
我不确定他/她是否希望该方法失败或者只是它没有被定义*,而是用于*'bool'。无论如何,你的解决方案是非常好的:) – 2010-11-11 21:39:52
这与John Dibling的答案相比如何?额外的“间接”是否有任何优势?谢谢 – icecrime 2010-11-11 21:50:42
@icecrime你不需要重复构造函数,它可以缩放。如果某个函数只对'int'有意义而对其他函数没有意义,则不需要另一个派生类。我喜欢为此使用重载。 – 2010-11-11 22:04:21