When I started as at my first job as software developer I was assigned to create a system for that allows writing and reading C++ values (or objects) from and to a PDF document. This required a system for mapping types to an id and vice versa. The codebase was huge and there were several 'layers' of code (basic framework layer, tools-layer, view-layers, etc..). The proposed solution was to add a header file to each layer with an enum that contains ids for types defined in the particular layer. To avoid conflicts, each the first value of each enum would start with a offset value (1000, 2000, 3000, ...).
As this system seemed a bit clumsy to me I decided to try something else, and came up with class for representing unique ids:
// UniqueId.h
class UniqueId
{
public:
UniqueId();
operator int() const;
private:
int mId;
static int sCounter;
};
// UniqueId.cpp
int UniqueId::sCounter = 0;
UniqueId::UniqueId()
{
mId = ++sCounter;
}
UniqueId::operator int() const
{
return mId;
}
Instead of an enum, now simply a list of UniqueId objects could be created without fearing for conflicting ids.
The person reviewing my code (software architect of the company) agreed with this at first. However, a day later he changed his mind and told me this system won't work. I vaguely remember him mentioning that there would be problems resulting from multiple compilation units.
Today, some four years later, I still don't understand what the problem could have been. For as far as I know, the static variable will only be defined in one compilation unit (the one for UniqeId.cpp).
So.. since StackOverflow is so abundant with veteran developers I'd like to take the opportunity to ask for some clarification. Would this indeed have been a problem? Or was my idea ok?