Having a default constructor is optional, but in some situations it often uses your class. If you do not provide any constructors, the compiler will generate a default constructor for you, which is equivalent to one with an empty initializer list and an empty function body.
When implementing a default constructor, it is usually best to make it as efficient as possible, since often the default constructor is not used or overwritten. For instance. Streaming: T t; std::cin >> t; T t; std::cin >> t; or creating a fixed array of things that need to be reassigned later T arr[100]; . For this reason, although it might seem obvious that the default constructor set DateTime to βnowβ if it involves a system call or other expensive operation to find out the current date, it is usually best not to do this for the default constructor.
If you didnβt have constructors at all, then there are many situations where initializing values ββwill initialize all your members anyway, for example:
// Explicit value-initialzation of dynamcially allocated DateTime DateTime* pdt = new DateTime(); // Value-initialized temporary FunctionTakesDateTime( DateTime() ); // Copy-initalization from a value-initialized temporary DateTime dt = DateTime();
If you provided a default constructor, but did not explicitly initialize all members of the class, and these members were of the POD type (for example, time_t and int ), then these members now remained uninitialized. To get the same effect for initializing a value as if you had no user-declared constructors, you would need to explicitly initialize all your members in your default constructor.
DateTime() : ticks(), days(), months() {}
This will be my preferred default constructor implementation. This means that building by default is still pretty cheap, but by default DateTime has a well-defined and easily recognizable value for debugging and diagnostic purposes.
While you might have an initialized boolean that allows you to have a βdeferred construct,β I would not recommend it. This adds a lot of overhead to the rest of the design class so that there is likely to be very little benefit. If the client wants to manipulate DateTime with non-standard values, then it must be up to the client to initialize them as necessary.