1 / 23

Typedefs & Enums

Typedefs & Enums. Tricks for naming things. Typedef. typedef creates a new name for existing type Does not create new types! typedef exitingType newName ; Ex: typedef int number ; number x = 10; //number really means int. Why Typedef. Good uses Ugly constructs:

kylee
Download Presentation

Typedefs & Enums

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Typedefs & Enums Tricks for naming things

  2. Typedef • typedefcreates a new name for existing type • Does not create new types! typedef exitingType newName; • Ex: typedefintnumber; number x = 10; //number really means int

  3. Why Typedef • Good uses • Ugly constructs: std::vector<std:pair<int, int> >::iterator myIt; std::vector<std:pair<int, int> >::iterator myOtherIt; Vs: typedef std::vector<std:pair<int, int> >::iterator VectorPairIterator; VectorPairIterator myIt; VectorPairIterator myOtherIt;

  4. Why Typedef • Good uses • Dealing with platform issues //On PC:typedefintint32;//On arduino processortypedeflong int32; //anywhere: int32 myNum; //int32 definitely has 32 bits

  5. Constant Issue • Constants = readable code

  6. Constant Issue • Don't protect us from stupidity:

  7. Enums • Enumerated Type • Custom data type with discrete set of possible values A weekday is something from:{Monday, Tuesday, Wednesday, Thursday, Friday}

  8. Enums • Syntax: enum newType {valueList}; • Sample enums: enum standing {FRESHMAN, SOPHOMORE, JUNIOR, SENIOR}; enum color {RED, BLUE, GREEN, BLACK, ORANGE};

  9. Enum Use • Enumusage • Can make variables of that type • Variables can only have given values enum standing{FRESHMAN, SOPHOMORE, JUNIOR, SENIOR}; standingstudentYear = FRESHMAN; standing student2Year = 12; //Error

  10. Enums • Pluses of constants, without dangers

  11. Enum Rules • Enum values must be valid identifiers • Start with letter, no special symbols, etc…

  12. Enum Rules • Enum values must be valid identifiers • Can't reuse identifiers

  13. Enum Values • Enums stored as integral values • Starting from 0: {Monday, Tuesday, Wednesday, Thursday, Friday} 0 1 2 3 4

  14. Enum Values • Enums stored as integral values • Can modify by assigning {Monday, Tuesday, Wednesday, Thursday, Friday} 10 11 30 31 32

  15. Enum Values • Only use assignment/value if represent logical value:

  16. Relational Operators • Relational operations work based on assigned values (order of enumerated values)

  17. Interacting: • Cin can't read into enum type • Read in string/int, use if:

  18. Interacting: • Cout will print as number • Print using if/switch/array

  19. Interacting: • Legal to switch based on enum:

  20. Integral Values • Math with enum results in an int

  21. Integral Values • Can static cast into an enum • Back to being dangerous

  22. Integral Values • Can static cast into an enum • Back to being dangerous • Better:

  23. Enum Passing • Work normally as parameteror return type:

More Related