1

I just read some code similar to next one:

enum 
{
    width = 123,
    height = 321,
    position_x = 234
    position_y = 432
};

...

Widget* w = CreateWidget(position_x, position_y, width, height);

Is there any reason to use enum in this case instead of macros or const values?

EDIT: Is it correct to use enum like this? Is this usage considered some kind of abuse in enum usage?

4 Answers 4

6

There are plenty of reasons not to use macros. The enum in the question is scoped and won't interfere with the same identifier used in different scopes, so you can for example, defined a member position_x in a class without the macro mangling your class definition.

Comparing the enum to a constant, there are people that prefer the enum as it is guaranteed that it will not add to the binary size of the executable. In the case of a constant, it may add (a bit, well, actually an int) to the size of the binary.

Sign up to request clarification or add additional context in comments.

Comments

3

No, there's no special reason to choose an enum over macros or const int values in this case.

Editorial note: It's certainly legal code to use enum in this fashion, but it is a bit strange looking at first glance.

3 Comments

Although there are plenty of reasons not to use macros.
In the general case, yes. In this specific example, it's hard to say why a preprocessor definition would be a problem.
It would be a problem even here: the preprocessor isn't constrained by the language's scoping rules, so the names couldn't be used for anything except the constants in any context. (Of course, you can work around this with the usual bodge of a special naming convention for macros.)
0

In this particular case, there doesn't appear to be any real deciding factor when choosing between an enum and constant values. Both are better than a macro however, and macros should be avoided in general.

More generally, there are some differentiating aspects between an enum and constants:

  1. enums are distinct types, which is more expressive than integral values
  2. you can take the address of a constant, but you can't take the address of an enum value

Comments

0

It may be more convenient to use enum than static const int as a class member.

class A
{
    static const int Foo = 42;
    // may have to define A::Foo somewhere
};

class B
{
    enum { Foo = 42 };
    // done
};

2 Comments

You'll only have to define the variable if it's odr-used (roughly speaking, if you take the address of it). If you don't need to do that, then it's no less convenient than the enum; and if you do, then you can't use the enum anyway.
@MikeSeymour: if you pass it to a function with const int& parameter, you odr-use it, but you still can use an enum in this case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.