0

I am trying to call a programmer defined default constructor inside of an overloaded 3 argument constructor. When I create an instance of my Date class, I pass it 3 arguments, the overloaded constructor then passes those params to a validate function. If the return value is false, I want to call my default constructor inside my overloaded constructor, but I get garbage values. 1/-858993460/-858993460 Is it okay to call constructor within constructor?

//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;
    }
}
henhen
  • 910
  • 2
  • 16
  • 34
  • It is allowed, but that's not how you do it. You use the initializer list (`: this()`) and it will be unconditionally called (however you can overwrite the values conditionally) – Ben Voigt Jun 25 '18 at 01:38
  • @BenVoigt you have to use `: Date()` instead of `: this()` – Remy Lebeau Jun 25 '18 at 01:40
  • Can you provide an example of using the 'this' pointer in my initializer list? So to call my default constructor inside my overloaded I do Date() and change my initializer list declartion to Date::Date() : Date()?? – henhen Jun 25 '18 at 01:40
  • @RemyLebeau: Ah right... it turns out that "it's the opposite syntax from the one you think" is not a very good way to remember, because eventually "the one you think" changes with experience. – Ben Voigt Jun 25 '18 at 01:42
  • @RemyLebeau: But doesn't that cause a change in meaning of existing valid code (e.g. when `C` inherits from `C`)? – Ben Voigt Jun 25 '18 at 01:43
  • @henhen: There is no "do `Date()`". Just `Date::Date(string m, int d, int y) : Date() { ... }` – Ben Voigt Jun 25 '18 at 01:44
  • So i have to pass it the 3 default arguments from within my overloaded constructor? If false { month = "Jan"; day = 1; year = 2018; Date(month, day, year)? Or do I do what it is suggested here https://stackoverflow.com/questions/29063703/calling-constructor-with-in-constructor-in-same-class. I was planning on not having any parameters for my default constructor and just initialize to whatever default values I wanted – henhen Jun 25 '18 at 01:47
  • @henhen: No, you can't call it from inside the constructor body. Only from the initialization list. – Ben Voigt Jun 25 '18 at 01:50
  • Okay anser post helps a little – henhen Jun 25 '18 at 01:51
  • 1
    I guess you mean the three argument constructor? The two constructor signatures are both overloads, so "overloaded constructor" doesn't actually tell me which you mean. This will work: `Date::Date(string m, int d, int y) : Date() { if (validateDate(m,d,y)) { month = m; day = d; year = y; } }` Everything will first get its default value, then inside the function you choose to either keep the default value or change it to the arguments. – Ben Voigt Jun 25 '18 at 01:54
  • @BenVoigt ahhh I see. That clear things up a lot. Thanks for the explaination! – henhen Jun 25 '18 at 01:56
  • @henhen If the default constructor just initialises member variables to constant values then you can get rid it altogether. Just initialise those members in the class definition instead (e.g. `class Date { ... int day = 1; int month = 1; int year = 1970; ...};`) – Paul Sanders Jun 25 '18 at 04:50
  • Is that common practice? I thought doing that was the job of the default programmer defined constructor @PaulSanders – henhen Jun 25 '18 at 17:39
  • @henhen I really couldn't say, but I personally like it a lot since when you add a new data member you're much less likely to forget to initialise it if you do it this way. – Paul Sanders Jun 26 '18 at 08:20

0 Answers0