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: 
I don't think there is a direct way to record the number of created objects without modifying the class as suggested by the other answers. There are however tools that can count the number of function invocations, so we can infer the number of objects from the number of constructor or destructor calls. Destructors are probably easier to count because they don't have overloads.

Tools that can do that include callgrind (part of the valgrind suite). Source-level instrumenters like gprof, TAU or Score-P would also be an option.

Probably only works if the class you're interested in actually defines a destructor/constructor though.
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.