c++11 vs c++ - enums differences -
//c++03 enum { s1 = 0, s2, scount }; int arr[scount]; //c++11 enum class { s1 = 0, s2, scount }; int arr[(int) something::scount];
how can use enums in case without casting count of enum int?
how can use enums in case without casting count of enum int?*
you can't... can remove class
enum class
the class
keyword means can't implicitly convert between enum
, int
removing class
keyword means enum
work in same way in previous versions of c++. won't need cast, loose safety of strongly typed enums
allowing implicit casts integral values.
//c++11 enum class { s1 = 0, s2, scount }; int arr[scount]; // errror //c++11 enum { s1 = 0, s2, scount }; int arr[scount]; // ok
you can read more strongly typed enums
here
other option cast it, doing. should avoid old
c-style cast
, use static_cast<>
instead has better type safety , more explicit. //c++11 enum class { s1 = 0, s2, scount }; int arr[static_cast< size_t > ( scount ) ]; // ok
Comments
Post a Comment