Skip to content

Instantly share code, notes, and snippets.

@fresky
Created January 3, 2014 05:20
Show Gist options
  • Save fresky/8233205 to your computer and use it in GitHub Desktop.
Save fresky/8233205 to your computer and use it in GitHub Desktop.
Use CRTP to record the object counter
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