I would consider the use of static
members for anything but const
values (that aren't eligible for constexpr
) to be a bad practice. The scope of a static member or method local is global, it just happens to be accessible only by (all instances of) a class.
Non-const class static is at the top of my list for bad practices. It's worse than simply declaring a global variable - because it's hidden. A singleton class (or even just a class that you construct only once and pass to dependent classes) eliminates this practice.
But otherwise I would recommend exposing the static via a global accessor (with big ugly comments around it). If at any point the code becomes concurrent, you can drop in a critical section.
// Danger, global state.
int inc_bar()
{
static int bar = 0;
return bar++;
}
class my_class
{
public:
my_class()
{
}
void inc()
{
printf("%d\n", inc_bar());
}
};