Implementing the default constructor

I am trying to implement a DateTime class in C ++:

class DateTime { public: DateTime(); DateTime(time_t ticks); DateTime(int day, int month, int year); DateTime(int day, int month, int year, int hour, int minute, int second); //... private: time_t ticks; int day; int month; //... } 

then in the application:

 DateTime date1; //default constructor 

I know that C ++ requires a default constructor, but how to implement it in this situation?

Should it set all properties to 0? This will make all other methods work, but it doesn't really seem intuitive ...

Should all properties be left uninitialized? It would not do any of its methods, but it seems more intuitive than 0, because you haven't done anything yet.

If it sets the internal bool initialized=false , then all methods check that before it works?

I am not sure about that. Is there a "standard" way to do this?

+4
source share
4 answers

Typically, the default constructor initializes you by default.

If you use time_t internally, setting it to time_t 0 ( Unix epoch , which is 1/1/1970) would be a smart option, since the values ​​"0" are the usual default values.

In this case, a default constructor is not required in C ++ - you may have a type without a default constructor, for which you need to specify a valid "time".

+9
source

C ++ for C ++ is not required. Unless you have a good idea of ​​how this should work, the odds are pretty good that you simply shouldn't have them. If you had it, the most obvious task would probably be to get the current date and time.

Edit: if you do not explicitly define any ctors, then the compiler will generate a default ctor and a copy of ctor for you. None of them are particularly bad: if you do not know how your data should be initialized, this can be perfectly reasonable.

As for the creation of the vector, it goes: no, you do not need ctor to put things into the vector by default. Typically, the default value ctor is used to initialize elements that are not assigned any value. For example, std::vector<DateTime> x(10); creates a vector of 10 DateTime objects, each of which is initialized with the default ctor value. If you don't have (and don't want) a default ctor, you can pass an instance of DateTime that will be used to initialize these objects:

 DateTime party_time(12, 31, 1999); std::vector<DateTime> x(10, party_time); 
+8
source

If you do not need a default constructor, you can declare a private default constructor so that it cannot be used.

+1
source

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.

+1
source

All Articles