1

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. ;-)

DevSolar
  • 67,862
  • 21
  • 134
  • 209
  • Do you want to hide the values from the human reader but not from the compiler, or what? – n. m. could be an AI May 06 '14 at 08:43
  • @n.m.: Basically, yes. (Now that you put it that way, it sounds doubly stupid. ;-) ) – DevSolar May 06 '14 at 08:43
  • 1
    Before program is compiled, pre processing takes place where numerical constants are substituted. So even if you obfuscate the constant some how, they will be replaced in preprocessing and every one will know what that constants are – coder hacker May 06 '14 at 08:44
  • Do you have access to C++11? Because if you do your should be using `constexpr` to specify that a value is a compile-time constant – RamblingMad May 06 '14 at 09:22

3 Answers3

2

To be usable in as switch statement labels the values have to be seen by the compiler earlier in the source of this translation unit.

So essentially, no, you can't declare symbolic constants without disclosing their values, and use them as labels in a switch.

However, you can use an if-else construction.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • Somehow I was hoping for something convoluted that could be hidden inside the implementation, leaving only a pristine header devoid of technical detail. But it seems you cannot have the cake *and* eat it. Ty. – DevSolar May 06 '14 at 08:58
0

You can hold method/function pointers mapped to T_Type somewhere, but yep, it's all just hacks out of problem which don't worth to be created in first place - hardcoded logic can only work with hardcoded values.

bigblackdot
  • 138
  • 1
  • 9
0

Your typedef declaration was wrong. What about this one?

typedef int T_Type;

// defined elsewhere

extern const T_Type T_FOO;
extern const T_Type T_BAR;

// elsewhere defined as, say
const T_Type T_FOO = 1; 
const T_Type T_BAR = 2;
Dr. Debasish Jana
  • 6,980
  • 4
  • 30
  • 69