Namespaces, Typedefs & Enums
310 likes | 335 Views
Learn tricks for naming entities to avoid collisions in libraries, utilizing namespaces to organize code effectively, and leveraging typedefs and enums to improve code readability and maintainability.
Namespaces, Typedefs & Enums
E N D
Presentation Transcript
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 • 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
Declaring in Namespace • Can invent any arbitrarynamespace like this
Using • Using can bring in… • Whole namespace • One identifier Let me use anything fromLibrary1 without Library1:: Let me use foo() fromLibrary1 without Library1::
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
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
Why Typedef • Bad uses • Renaming int to number
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;
Why Typedef • Good uses • Dealing with platform issues //On PC:typedefintint32;//On arduino processortypedeflong int32; //anywhere: int32 myNum; //int32 definitely has 32 bits
Constant Issue • Constants = readable code
Constant Issue • Don't protect us from stupidity:
Enums • Enumerated Type • Custom data type with discrete set of possible values A weekday is something from:{Monday, Tuesday, Wednesday, Thursday, Friday}
Enums • Syntax: enum newType {valueList}; • Sample enums: enum standing {FRESHMAN, SOPHOMORE, JUNIOR, SENIOR}; enum color {RED, BLUE, GREEN, BLACK, ORANGE};
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
Enums • Pluses of constants, without dangers
Enum Rules • Enum values must be valid identifiers • Start with letter, no special symbols, etc…
Enum Rules • Enum values must be valid identifiers • Can't reuse identifiers
Enum Rules • Enum values must be valid identifiers • Can't reuse values • Enum type names should be singular • Don't want:grades JohnsGrade;
Enum Values • Enums stored as integral values • Starting from 0: {Monday, Tuesday, Wednesday, Thursday, Friday} 0 1 2 3 4
Enum Values • Enums stored as integral values • Can modify by assigning {Monday, Tuesday, Wednesday, Thursday, Friday} 10 1130 3132
Enum Values • Only use assignment/value if represent logical value:
Relational Operators • Relational operations work based on assigned values (order of enumerated values)
Interacting: • Cin can't read into enum type • Read in string/int, use if:
Interacting: • Cout will print as number • Print using if/switch/array
Interacting: • Legal to switch based on enum:
Enum’s & Math • Math with enum results in an int
Enum’s & Math • Can static cast into an enum • Can go out of range!
Enum’s & Math • Can static cast into an enum • Can go out of range! • Prevent:
Enum’s & Math • Looping with enum:
Enum’s & Math • Work normally as parameteror return type: