![]() To make a scoped enumeration, we use the keywords enum class. Scoped enumerations work similarly to unscoped enumerations ( 10.2 - Unscoped enumerations), but have two primary differences: They are strongly typed (they won’t implicitly convert to integers) and strongly scoped (the enumerators are only placed into the scope region of the enumeration). That solution is the scoped enumeration (often called an enum class in C for reasons that will become obvious shortly). With standard enumerators, there’s no easy way to prevent this.īecause of such challenges, as well as the namespace pollution problem (unscoped enumerations defined in the global scope put their enumerators in the global namespace), the C designers determined that a cleaner solution for enumerations would be of use. This doesn’t make sense semantically since color and fruit are from different enumerations and are not intended to be comparable. Since color and fruit are both set to enumerators that convert to integer value 0, color will equal fruit. Eventually the compiler will determine that if it converts both to integers, it can do the comparison. Next, it will try converting Color and/or Fruit to integers to see if it can find a match. When color and fruit are compared, the compiler will look to see if it knows how to compare a Color and a Fruit. ![]() Std::cout << "color and fruit are not equal\n" Std::cout << "color and fruit are equal\n" // and find they are equal! If (color = fruit) // The compiler will compare color and fruit as integers The first example returns the 1st indexed field ('2' has an index value of 1, as it's defined first), while the second example returns the string value '1'.Although unscoped enumerations are distinct types in C , they are not type safe, and in some cases will allow you to do things that don’t make sense. It's easy to get confused between returning the enum integer with the stored value, so we don't suggest using ENUM to store numerals. Sorting is according to the index value: CREATE TABLE enums ( a ENUM ( '2', '1' )) INSERT INTO enums VALUES ( '1' ),( '2' ) SELECT * FROM enums ORDER BY a ASC - | a | - | 2 | | 1 | - Selecting by numeric index: SELECT * FROM fruits WHERE fruit = 2 - - - | id | fruit | bushels | - - - | 3 | orange | 25 | - - - ![]() We suggest not using ENUM to store numerals, as there is little to no storage space benefit, and it is easy to confuse the enum integer with the enum numeral value by leaving out the quotes.Īn ENUM defined as ENUM('apple','orange','pear') would have the following index values:Įxamples CREATE TABLE fruits ( id INT NOT NULL auto_increment PRIMARY KEY, fruit ENUM ( 'apple', 'orange', 'pear' ), bushels INT ) DESCRIBE fruits - - - - - - | Field | Type | Null | Key | Default | Extra | - - - - - - | id | int ( 11 ) | NO | PRI | NULL | auto_increment | | fruit | enum ( 'apple', 'orange', 'pear' ) | YES | | NULL | | | bushels | int ( 11 ) | YES | | NULL | | - - - - - - INSERT INTO fruits ( fruit, bushels ) VALUES ( 'pear', 20 ), ( 'apple', 100 ), ( 'orange', 25 ) INSERT INTO fruits ( fruit, bushels ) VALUES ( 'avocado', 10 ) ERROR 1265 ( 01000 ): Data truncated for column 'fruit' at row 1 SELECT * FROM fruits - - - | id | fruit | bushels | - - - | 1 | pear | 20 | | 2 | apple | 100 | | 3 | orange | 25 | - - -
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |