我试图在重载的3参数构造函数内部调用程序员定义的默认构造函数。当我创建Date类的实例时,将其传递3个参数,然后重载的构造函数会将这些参数传递给validate函数。如果返回值为false,我想在重载的构造函数中调用默认的构造函数,但会得到垃圾值。
1/-858993460/-858993460
可以在构造函数内调用构造函数吗?
//test.cpp
int main ()
{
date d1(m, d, y);
}
//header
class Date {
private:
string month;
int day, year;
bool validateDate(string, int, int);
//Date();
public:
Date();
Date(string, int, int);
void print(DateFormat type);
};
//implmentation
Date::Date() : month("January"), day(1), year(2001) { cout << "INSIDE CONST" << endl; } //default constructor
Date::Date(string m, int d, int y) //overloaded constructor
{
if (!validateDate(m, d, y))
{
cout << "IF FALSE" << endl;
//Date(); //This doesn't work
Date d1; //This doesn't work as well
}
else
{
month = m;
day = d;
year = y;
cout << "MONTH IS :" << month << " DAY IS: " << day << " YEAR IS: " << year << endl;
}
}