how could the compiler differentiate count variable for different objects?
It can't. Local static variables have nearly the same semantics as global variables. The only difference is that they can't be used by other functions, and that they are constructed when their functions are first called (if a function that contains a static is never call, that static is never constructed).
Opinions differ, but when I need to use static variables, I try to go from less visibility (fewer dependencies) to more:
1. static variable inside a method/function (C-style)
2. static variable in a module (.cpp) - doesn't show up in .hpp so less dependency between modules (C-style)
3. static variable in a class (C++-style)
The general idea is, you want to limit the scope of that static (global) variable as much as possible.
If you are writing multithreading code, you want to avoid static variables as much as possible.