我想模板一个类的属性,但不是所有的功能。C++ - 如何模板类属性,而不是类的功能?
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
为什么向下票?诚然,它可以变得更清楚,但仍然是别人可以从中受益的问题。 – StoryTeller 2013-02-17 10:12:15