about enumeration

enum SchedType {OUTDEG=0, SQRT_OUTDEG, UPLOAD_BW, SQRT_UPLOAD_BW};

This is a statement written by a professional engineer, I don't know the reason why he initialize OUTDEG=0

besides, I found that OUTDEG, SORT_OUTDEG could be changed by statement like int OUTDEG=4;
so how to keep it unchangable?


Last edited on
closed account (S6k9GNh0)
When an enum is made, the first constant in a list is 0 and then the following constant is n + 1.

Same as doing:

1
2
3
4
#define OUTDEG 0
#define SQRT_OUTDEG 1  //Equal to: enum {OUTDEG, SQRT_OUTDEG, UPLOAD_BW, SQRT_UPLOAD_BW};

#define UPLOAD_BW 2 


The reason you cannot change or redefine OUTDEG or any member of SchedType is simply because all of the members are made constant. That's the purpose of enum is a list of constant values.

And to answer your question, there is no reason to assign it the default value of 0. It's made 0 when declared.
Last edited on
MY question is not phrased before, and now I have updated it .
hope for new answers!
It's value can be assigned while declaration. You can start it from any value you want.
Consider the following examples:

Example 1:

enum {BLUE, GREEN};

Value BLUE = 0; // Default value if not specified
GREEN = 1;

Example 2:

enum {BLUE = 12, GREEN};

Value BLUE = 12; // Assigned value
GREEN = 13; // Since value of the element before this is 12 and the value of this has not been assigned. Therefore,
// prev_val+1

Example 3:

enum {BLUE = 19, GREEN = 10};

Value BLUE = 19; // Assigned value for BLUE
GREEN = 10; // Assigned value for GREEN

Example 4:

enum {BLUE=12, GREEN=8, YELLOW};

Value BLUE = 12; // Default value if not specified
GREEN = 8;
YELLOW= 9; // Again, since not specified, therefore prev_val + 1


Hope this helps !
closed account (z05DSL3A)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
enum SchedType {OUTDEG=0, SQRT_OUTDEG, UPLOAD_BW, SQRT_UPLOAD_BW};

void test(SchedType t)
{
    return;
}
int main()
{
    // This line will give you an error
    OUTDEG = 3;
    
    test(OUTDEG); // calls with SchedType::OUTDEG
    
    // This line creates a new variable
    // called OUTDEG nothing to do with SchedType::OUTDEG
    // but will hide it
    int OUTDEG = 3;

    //this line will now give you an error
    test(OUTDEG);
    
    return 0;
}
thanks!
Topic archived. No new replies allowed.