すべての関数ではなく、クラスの属性をテンプレート化したいと考えています。
enum myEnum
{
CHAR,
INT,
FLOAT,
DOUBLE
};
class myClass
{
public:
myClass(T);
~myClass();
myEnum getType(); // need to template it to know the type
myClass *operator+(const myClass&);
/*
I don't want to template it, because I don't need it,
I can find the type tanks to getType() (for the precision of the operation,
I'll need it, ie. for a char + int)
*/
protected:
T _value;
std::string _sValue;
};
クラスで一意の関数をテンプレート化する方法を知っていますtemplate<typename T>
。クラスの関数の上に書くだけです。T _value
すべてのクラスをテンプレート化せずに属性をテンプレート化する方法を知りたいです。
属性に対して同じことをしようとすると、次のようになります。
template<typename T>
T _value;
私はそのエラーがあります:
error: data member '_value' cannot be a member template
error: ‘myClass’ is not a template