Question: How can I instrument C++ code such that I can get the object count per class at runtime?

I could overload the new and delete operators, implement custom constructors/destructors, but I am looking for a generic approach that does not require altering all classes in my code.

Answer: 
One way is to have a static `count` member on the class. You would have to increment it each time a constructor is called, and decrement when destructor is called.

E.g.

```
class SomeClass {
  private:
    static uint64_t count;

  public:
    SomeClass() {
        ++count;
    }
    ~SomeClass() {
        --count;
    }
};

// Initialize count
uint64_t SomeClass::count = 0;

```
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.