class Date
{
private:
int Day;
int Month;
int Year;
bool CheckDate(int InputDay, int InputMonth, int InputYear);
// this return true when the date is valid
public:
Date(int InputDay, int InputMonth, int InputYear);
~Date();
};
Date::Date(int InputDay, int InputMonth, int InputYear)
{
if (!CheckDate(InputDay, InputMonth, InputYear))
{
cout << "Date Invalid!\n";
this->~Date();
// this invokes the destructor
// however at the end of the program the destructor would invoke again
}
else
{
Day = InputDay;
Month = InputMonth;
Year = InputYear;
}
}
I find a resource here How can object construction of a class be stopped if a parameter passed is found to be wrong?. Is there a way to do it without exception? Is there a way which the constructor check the parameter itself and destruct itself?