Object Counter
The main purpose of an object counter is retrieving statistics of object creation and destruction for a given class. This can be easily solved using CRTP:
templateEach time an object of class X
is created, the constructor of counter
is called, incrementing both the created and alive count. Each time an object of class X
is destroyed, the alive count is decremented. It is important to note that counter
and counter
are two separate classes and this is why they will keep separate counts of X
's and Y
's. In this example of CRTP, this distinction of classes is the only use of the template parameter (T
in counter
) and the reason why we cannot use a simple un-templated base class.
Read more about this topic: Curiously Recurring Template Pattern
Famous quotes containing the words object and/or counter:
“Anxiety is not fear, being afraid of this or that definite object, but the uncanny feeling of being afraid of nothing at all. It is precisely Nothingness that makes itself present and felt as the object of our dread.”
—William Barrett (b. 1913)
“As deaths have accumulated I have begun to think of life and death as a set of balance scales. When one is young, the scale is heavily tipped toward the living. With the first death, the first consciousness of death, the counter scale begins to fall. Death by death, the scales shift weight until what was unthinkable becomes merely a matter of gravity and the fall into death becomes an easy step.”
—Alison Hawthorne Deming (b. 1946)