It is common practice to define symbolic constants in a header file:
#define T_FOO 1
#define T_BAR 2
Ugly.
static const int T_FOO = 1;
static const int T_BAR = 2;
Better, since not preprocessor.
enum
{
T_FOO = 1,
T_BAR
} T_Type;
Better still, since T_Type
carries information of purpose, and the compiler can do additional checks (e.g. if all cases are handled in a switch
).
There's probably half a dozen more variants. One thing though... they all disclose numerical values to the client. I'd like to keep those values hidden, simply because they shouldn't matter. But the one way I could think of...
typedef int T_Type;
// defined elsewhere
extern const T_Type T_FOO;
extern const T_Type T_BAR;
...does not work for e.g. case
statements (as T_FOO
and T_BAR
are constants, but not a compile-time constant expressions).
Is there a way to have it all?
- Declaring symbolic constants in a header without disclosing numerical values,
- but useable as constant expressions e.g. in
switch
statements?
My level of understanding says "no", but I know that I don't know everything. ;-)