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); LibA::checkPositive(int x) LibA::somethingElse() LibA::x LibB::checkPositive(int x) LibB::otherThing()
Declaring in Namespace Can invent any arbitrary namespace like this
Using Using can bring in… Whole namespace One identifier Let me use anything from Library1 without Library1:: Let me use foo() from Library1 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 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 Bad uses Renaming int to number
Why Typedef Good uses Vs: 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;
Why Typedef Good uses Dealing with platform issues //On PC: typedef int int32; //On arduino processor typedef long 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 Enum usage Can make variables of that type Variables can only have given values enum standing {FRESHMAN, SOPHOMORE, JUNIOR, SENIOR}; standing studentYear = 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 11 30 31 32
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 parameter or return type: