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.
Instead of overloading you could try defining a macro which redefines all the new keywords in the entire project.

```
#define new (some code to create and log)

```