J
JKop
Here's a little pickle I've been pondering over. I've simplified it down for
posting, here goes:
Consider a function as simple (and as dumb) as the following:
bool IsAfter1998(Year year)
{
return (year > 1998);
}
So let's define the "Year" type. We start off with:
typedef int Year;
I want to restrict the value of a "Year" to not being 0 ( 1 BC is followed
by 1 AD). I've started off with this:
class Year
{
private:
int data;
void Set(int year)
{
if ( year == 0 ) throw bad_year();
data = year;
}
public:
class bad_year {};
explicit Year(int year = 1)
{
Set(year);
}
Year& operator=(Year year)
{
Set(year);
return *this;
}
operator int()
{
return data;
}
};
I figured a lot of people have probably done something like this before. So
can anyone suggest any suitable alternatives or maybe tweek my code a
little?
Particularly with my code, I realize that one can't do the following:
Year year;
++year;
-JKop
posting, here goes:
Consider a function as simple (and as dumb) as the following:
bool IsAfter1998(Year year)
{
return (year > 1998);
}
So let's define the "Year" type. We start off with:
typedef int Year;
I want to restrict the value of a "Year" to not being 0 ( 1 BC is followed
by 1 AD). I've started off with this:
class Year
{
private:
int data;
void Set(int year)
{
if ( year == 0 ) throw bad_year();
data = year;
}
public:
class bad_year {};
explicit Year(int year = 1)
{
Set(year);
}
Year& operator=(Year year)
{
Set(year);
return *this;
}
operator int()
{
return data;
}
};
I figured a lot of people have probably done something like this before. So
can anyone suggest any suitable alternatives or maybe tweek my code a
little?
Particularly with my code, I realize that one can't do the following:
Year year;
++year;
-JKop