The easiest is to have a static member that counts the number of instances.
The constructors need to increment them, and the destructor will decrement it.
To access it, you will need a public static getter.
class C
{
private:
static size_t c;
public:
C() { c++; }
C( const C& other )
{
c++;
*this = other;
}
~C() { c--; }
static size_t getNb()
{
return c;
}
};
// counter instanciation
size_t C::c = 0;
This could possibly be templated, but it has limitations, as you can't have specific constructors/destructors. See linked answer for a more generic approach.