Presentation is loading. Please wait.

Presentation is loading. Please wait.

Namespaces How Shall I Name Thee?.

Similar presentations


Presentation on theme: "Namespaces How Shall I Name Thee?."— Presentation transcript:

1 Namespaces How Shall I Name Thee?

2 Name Collision by default, function, class, global constant and variable scope is global in large programming projects multiple people may give different constructs the same name, resulting in name collision. words like sort, find, count and print are tempting name collision may result in compile-time error - when header is included linker error namespaces – designed to sub-divide global scope and avoid name collision

3 Defining Namespaces define a namespace block, declare constructs inside namespace myNamespace{ const int limit=5; void myFunc(int a){ ++a; } functions, classes, etc. (constructs with separate definitions) may be defined outside void myFunc(int); void myNamespace::myFunc(int a){ thus structured, the namespace is non-executable and should go into header file

4 Using Namespaces several styles
always use scope resolution operator (explicit statement of scope) for(int i=0; i < myNamespace::limit; ++i) myNamespace::myFunc(i); import a name – specify that you are using a specific name in this file using myNameSpace::limit; using myNameSpace::myFunc; for(int i=0; i < limit; ++i) myFunc(i); import all names in a namespace using namespace myNamespace; do not do in headers – opens namespace for all source files including this header

5 Scope of Importing variables can be imported either into global scope or into a block importing into the global scope – imported variables can be used anywhere using myNameSpace::limit; int main(){ for(int i=0; i < limit; ++i) myFunc(i); } importing into the scope of a function – imported names can be used inside function only

6 std Namespace has an extensive set of names used in programs. Contains
cout, cin, endl, vector, string, etc. three styles of using std: explicit scope resolution of all names pro: safest con: program code becomes less terse import specific names with using pro: more terse con: have to maintain name list at beginning of the file import all names using namespace std; pro: simplest, tersest con: unexpected names are imported: sort, find, count are in std; do not use in header files!

7 Namespaces Review what is the scope of a function? global variable? global constant? what is name collision? what is a namespace and why is it needed? how to define a namespace? is namespace executable statement? how do you define a function declared in a namespace? the three styles of using namespaces are explicit namespace resolution importing specific name importing all names in a namespace how do you using each style? what are the advantages/disadvantages of each style? which style should be used in a header what happens when using is put inside/outside function definition?


Download ppt "Namespaces How Shall I Name Thee?."

Similar presentations


Ads by Google