I'm getting to know traits in C++. I've got these classes:
class Basic_traits {
public:
...
static const int prec = 0;
};
template<typename T>
class Traits : public Basic_traits {
public:
...
};
class Temperature {
double t;
public:
Temperature(double temp = 50) : t(temp) {};
double operator()() { return t; };
Temperature& operator=(double temp) { t = temp; return *this; };
};
template<>
class Traits<Temperature> : public Basic_traits{
public:
...
static const int prec = 2;
friend ostream& operator<<(ostream& os, const Temperature&) {
os.precision(prec);
}
};
What I want to do here is make an overloaded << operator for class Temperature, which will cout the t variable with the precision given in the prec trait. Is this even possible? If so, then how do i do it? Is the operator supposed to be in the template? I put it there cause that's where the prec is and class Temperature itself doesn't have anything to do with Basic_traits which contains a variable prec.
////////////////////////////////////////////////////////////////////////////////
My operator now looks like this:
friend ostream& operator<<(ostream& os, const Temperature&) {
os.precision(Traits<Temperature>::prec);
return os << (Temperature);
}