[Solved] Static member variable assignment in a class constructor [closed]


It is only a logical error if you consider it to be one.
If however the change of the value of the static variable with each constructor is exactly what you WANT it to do, then it is not a logical error.
As in the comments already mentioned, one example is to count all constructors. This is often accompanied by also counting all DEstructors and in the end yields a count of existing instances.

The example could be implemented by using the = operator on the static variable,
count_of_ctors = count_of_ctors +1;;
though it probably would usually use ++,
count_of_ctors++;.

In the special case outlined in the question, setting a variable to a value given as parameter to the ctor, it could achieve the same, i.e. by giving the current value of the static variable, increased by one.

But you probably meant a value which is not derived from the current value of the static variable.
That still could be intended behaviour. I imagine that recording the last date+time of an instantiation could be such a value, which could serve debugging or logging purposes. That would be used by instantiating always with the current time.

Since the current time would probably better be read by the ctor (for convenience and for tampering protection), here is another idea. For each instantiation, the ID of the user (and maybe a matching passwort) has to be given, so that the static variable always has the last user ID which created an instance.

solved Static member variable assignment in a class constructor [closed]