mardi 2 février 2016

Is it a good idea to use classes just to maintain few constant variables

I came across a code for logging mechanism in which based on severity the messages are printed. So, if the severity is set to 0 then print DEBUG logs, if 1000 then print only INFO logs etc. etc.

class Logs
{
public:
    static const int DEBUG=0;
    static const int INFO=1000;
    static const int SOMETHING_ELSE=2000;
}

That's it in the class. Nothing else. And instead of using values as it is, the variables are used. Example : If i wanted to pass severity to some helper function then instead of passing 1000 as its argument it passes Logs::INFO.

My question is that why do we really need a class just to define these const variables. We could have used enums as well. Or Macros. Or const global variables. Or const variables inside namespaces. But why classes? Are there any advantages of using classes over the other options that i mentioned?

Is it just matter of choice to use one over the other or one has a concrete advantage over the others?

Aucun commentaire:

Enregistrer un commentaire