I saw the following piece of code, and I have problems to understand how it works.
class Month { public: static const Month Jan() { return 1; } ... static const Month Dec() { return 12; } int asInt() const { return monthNumber; } private: Month(int number) : monthNumber(number) {} const int monthNumber; }
The class is designed so that the user does not receive an invalid month value.
Here's the question: why can a static Jan function return 1 with a return value like a month?
thanks
Based on the comments, this class can be constructed as follows:
class Month { public: static const Month Jan() { return Month(1); } ... static const Month Dec() { return Month(12); } int asInt() const { return monthNumber; } private: explicit Month(int number) : monthNumber(number) {} const int monthNumber; };
q0987 source share