Presentation is loading. Please wait.

Presentation is loading. Please wait.

Namespaces, Typedefs & Enums

Similar presentations


Presentation on theme: "Namespaces, Typedefs & Enums"— 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); LibA::checkPositive(int x) LibA::somethingElse() LibA::x LibB::checkPositive(int x) LibB::otherThing()

4 Declaring in Namespace
Can invent any arbitrary namespace like this 

5 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::

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

8 Why Typedef Bad uses Renaming int to number

9 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;

10 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

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

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}

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

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 parameter or return type:


Download ppt "Namespaces, Typedefs & Enums"

Similar presentations


Ads by Google