0

Possible Duplicate:
Should I use #define, enum or const?
Advantage and disadvantages of #defines vs. constants?

How enum will be more useful than #define and const. memory and code visibilty point of view and readability point of view. can I convert (type cast) enum to array of int, If I have taken all value within integer.

Example:

    class MapPlt_clContainer { 
    public:  
    enum tagVectorType {       
    enVTAll = 0,       
    enVTPolygon,       
    enVTLines 
    }  
    };  

    tVoid MapPlt_clRender::vDrawLineClass( MapPlt_clContainer::tagVectorType* ) 

While calling function enum pass

vDrawLineClass( ClassArray_Group ); //Working

While calling array base address pass

int test[3] =  
{       
5,       
6,       
7, 
};   

vDrawLineClass( test); //Not Working

Error!!

Should it type cast it automatically? or it is compiler dependent. In my case it is giving error.

Community
  • 1
  • 1
kapilddit
  • 1,729
  • 4
  • 26
  • 51
  • 3
    FWIW, http://stackoverflow.com/questions/112433/should-i-use-define-enum-or-const is tagged as C++, and this question is tagged as C, and there is a distinct difference in how `const` works between the two. – jamesdlin Jul 25 '12 at 11:38
  • 1
    Also: there are no classes in c, and the `public:` label is not allowed in a struct (or union) definition. `::` is always a syntax error in C, and the parentheses in `&(ClassArray_Group[5])` are not needed, which implies that the interviewer is not sure about his syntax. – wildplasser Jul 25 '12 at 12:13
  • Sorry, I made very confusing code to demostrate this senario. but I want to know if I pass array base address to function which is expecting enum address then will it work? – kapilddit Jul 25 '12 at 12:18

3 Answers3

4

enum is a separate type unlike #define and the language (with the help of compiler) will help you ensure you are not mixing values of different types (even if they are of the same numerical value).

Additionally the value of an enum is available to the debugger whereas the original meaning of the #define is lost during the pre-processing time (before the code generation has even begun).

Type-casting an enum to an int is an automatic built-in process while the opposite conversion is trickier as not all the int values could be valid for your particular enum.

Modern compilers will also warn you if you have used all the possible enum's values in a switch statement that has no default clause, something that cannot be checked for #defines

YePhIcK
  • 5,816
  • 2
  • 27
  • 52
1

If you are using an enum as an integer in C++ you have a smell. An enum defines a type, and only the values of that type should be used. (I realize this isn't enforced and the enum can be interpreted as an int, but with C++ it generally shouldn't).

Also, a big pet peeve of mine: Don't put "Type" in the name for an enum in C++. The values of an enum are not "types" (in the C++ sense of the word). As soon as you start doing template code, you will HATE all the enums with the word Type in their type name.

Also, any time you are trying to typecast in your design, you are doing it wrong. That is an awful smell in C++. You shouldn't have to do it, and you certainly shouldn't design it into your code (i.e. use it as a "feature").

Finally, this part:

int test[3] =  
{       
5,       
6,       
7, 
}; 

vDrawLineClass( test); //Not Working

This is a straight up ugly hack. Do what you say, say what you do:

MapPlt_clContainer::tagVectorType test[3] =  
{       
MapPlt_clContainer::enVTAll,       
MapPlt_clContainer::enVTPolygon,       
MapPlt_clContainer::enVTLines 
};

vDrawLineClass( test);
Josh Petitt
  • 9,371
  • 12
  • 56
  • 104
1

In addition to the points made in other answers, I would like to add the following:

If you have multiple types and you need to iterate on them, you will have to use an array of constants, which will be something like this:

const int states[] = {STATE_1,STATE_2, STATE_3, STATE_4 };
int numStates = sizeof(states)/sizeof(state[0]);
for (int i = 0; i < numStates; i++) {
    // Do something with states[i]..
}

With enumerations, this can be simplified as

enum states{cState_1 = 0, cState_2, cState_3, cState_4, cNumStates};
for (int i = 0; i < numStates; i++) {
    // do something with i
}
sud03r
  • 19,109
  • 16
  • 77
  • 96