I am preferring to remove all the printing stuff from the class interface and use the operator<< overloading idea in 200_success answer like this:
#include <iostream>
enum thing_type
{
DTypeUnknown,
DTypeAnimal,
DTypeMineral,
DTypeVegetable
};
const char* type2string(thing_type ttype) {
static const char* thtype[] = {
"Unknown",
"Animal",
"Mineral",
"Vegetable"
};
return ttype < sizeof(thtype) ? thtype[ttype] : thtype[0];
}
std::ostream& operator<<(std::ostream& os, const thing_type type) {
os << type2string(type);
return os;
}
class thing {
public:
thing(thing_type type) : type_(type) {}
const thing_type get_type() const { return type_; }
private:
thing_type type_;
};
std::ostream& operator<<(std::ostream& os, const thing& th) {
os << "This is a " << type2string(th.get_type());
return os;
}
int main() {
thing th(DTypeMineral);
std::cout << th << std::endl;
return 0;
}