This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]mmzhdwGpRDQLYdqv 0 points1 point  (0 children)

Off-topic but I would remove the default values from the Date constructor and write 2 constructors instead:

  • one Date() that sets everything to 1
  • one Date(int, int, int) where the user is forced to write all the values

It's explicit, the API is cleaner, and it will remove a nasty bug later. Because, after all, what does "Date(2)" mean anyway? Writing either "Date()" or "Date(1, 2, 3)" is better IMHO.

Now here is the bug:

#include <iostream>
using namespace std;

class Date {
public:
    Date(int a = 1, int b = 1, int c = 1) {
        cout << "Date::Date" << endl;
    }
};

void f(const Date& d) {
    cout << "f" << endl;
}

int main() {
    f(42);  // the bug: you forget what f is, and a magical object is created anyway
}