Created
January 3, 2014 05:20
-
-
Save fresky/8233205 to your computer and use it in GitHub Desktop.
Use CRTP to record the object counter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template <typename T> | |
struct counter | |
{ | |
static int objects_created; | |
static int objects_alive; | |
counter() | |
{ | |
++objects_created; | |
++objects_alive; | |
} | |
counter(const counter&) | |
{ | |
++objects_created; | |
++objects_alive; | |
} | |
protected: | |
~counter() // objects should never be removed through pointers of this type | |
{ | |
--objects_alive; | |
} | |
}; | |
template <typename T> int counter<T>::objects_created( 0 ); | |
template <typename T> int counter<T>::objects_alive( 0 ); | |
class X : counter<X> | |
{ | |
// ... | |
}; | |
class Y : counter<Y> | |
{ | |
// ... | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment