User-Defined Constructors and the Use of static opCall()
Let’s discuss user-defined constructors and the use of the static Opcall() function.
User-defined constructors
You have already learned about the behavior of the compiler-generated constructor. Since that constructor is suitable for most cases, there is no need to define a constructor by hand.
Still, there are cases where constructing an object involves more complicated operations than assigning values to each member in order. Let’s consider this example:
struct Duration {
int minute;
}
The compiler-generated constructor is sufficient for this single-member struct
:
time.decrement(Duration(12));
Since that constructor takes the duration in minutes, the programmers would sometimes need to make calculations:
// 23 hours and 18 minutes earlier
time.decrement(Duration(23 * 60 + 18));
// 22 hours and 20
...