enum and const difference

i have
enum{x=20,y=30,z=40};
and
const int x=20;
const int y=30;
const int z=40;

ia there a difference
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main( int argc, char* argv[] )
{
   enum{ x = 20 } ; // x is equivalent to a literal int
   // auto px = &x ; // *** error, x is not an lvalue

   const int y = 30 ; // y is not modifiable;
                      // here, it is a constant whose value is known at compile time
   auto py = &y ; // fine, y is an lvalue

   const int y1 = argc ; // y1 is not modifiable;
                         // here, it is a not constant whose value is known at compile time
   auto py1 = &y1 ; // fine, y1 is an lvalue

   constexpr int z = 30 ; // z is a constant whose value must be known at compile time
   auto pz = &z ; // fine, z is an lvalue
}
Any object of an unscoped enumeration can be implicitly converted to an integer type. However an object of an integer type may not be directly assigned to an object of unscoped enumeration. For example

enum Number {x=20,y=30,z=40};

Number n = x;

int v = n; // O'k: v = 20

n = v; // error: may not assign v to n

n = static_cast<Number>( v ); // O'k

Also you may overload operators for enumeration types.
Last edited on
Topic archived. No new replies allowed.