1 / 31

Namespaces, Typedefs & Enums

Namespaces, Typedefs & Enums. Tricks for naming things. Name Collisions. Including lots of libraries… Library A defines: bool checkPositive(int x); Library B defines: bool checkPositive(int y);. Name Spaces. Namespaces break names into groups

oshields
Télécharger la présentation

Namespaces, 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. Namespaces, Typedefs & Enums Tricks for naming things

  2. Name Collisions • Including lots of libraries… • Library A defines:bool checkPositive(int x); • Library B defines:bool checkPositive(int y);

  3. Name Spaces • Namespaces break names into groups • To use something from namespace, specify namespace to resolve it in:LibA::checkPositive(10); LibB::checkPositive(int x) LibB::otherThing() LibA::checkPositive(int x) LibA::somethingElse() LibA::x

  4. Declaring in Namespace • Can invent any arbitrarynamespace like this 

  5. Using • Using can bring in… • Whole namespace • One identifier Let me use anything fromLibrary1 without Library1:: Let me use foo() fromLibrary1 without Library1::

  6. Not Using Using • Best practice : don't include whole std namespace in .h files • std:: has lots of names • Anyone including your .h now stuck with them

  7. 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

  8. Why Typedef • Bad uses • Renaming int to number

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

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

  11. Constant Issue • Constants = readable code

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

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

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

  15. 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

  16. Enums • Pluses of constants, without dangers

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

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

  19. Enum Rules • Enum values must be valid identifiers • Can't reuse values • Enum type names should be singular • Don't want:grades JohnsGrade;

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

  21. Enum Values • Enums stored as integral values • Can modify by assigning {Monday, Tuesday, Wednesday, Thursday, Friday} 10 1130 3132

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

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

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

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

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

  27. Enum’s & Math • Math with enum results in an int

  28. Enum’s & Math • Can static cast into an enum • Can go out of range!

  29. Enum’s & Math • Can static cast into an enum • Can go out of range! • Prevent:

  30. Enum’s & Math • Looping with enum:

  31. Enum’s & Math • Work normally as parameteror return type:

More Related