Download presentation
Presentation is loading. Please wait.
1
Namespaces CSCE 121 J. Michael Moore
Based on Slides created by Carlos Soto.
2
Disambiguation Consider email Email address: president
Which one? address: webmaster Clarification: Clarification:
3
Disambiguation in C++ Function signatures must be unique
Lots of people may want to create an ‘add’ function… How to give it unique signature?
4
Namespace Can put related things together.
Class name becomes a namespace for its attributes and methods (aka. data members and member functions) Can explicitly create a namespace.
5
Scope Everything that has a name in C++ (functions, objects, classes, etc.) has a scope global scope class scope local scope function scope (including inside main) statement scope A namespace is just a named scope
6
Recall :: Scope resolution operator
For classes, it means that the identifier that follows (data or function) is a member of the class that precedes the :: So for Student::getID(), getID() is a member function of the class Student
7
std? vector and string are not member functions or data members, they’re datatypes... and std is not a class... it’s a namespace
8
namespace std namespace std { class string // ... class vector // ... ostream cout; istream cin; // ... and more! }
9
Namespaces namespace apple { int golden = 1; } namespace orange { char navel = ‘n’; cout << apple::golden; cout << orange::navel;
10
Namespaces namespace apple { bool ripe = false; } namespace orange { bool ripe = true; bool a = apple::ripe; bool b = orange::ripe;
11
What we normally do #include <vector> #include <string> using namespace std; int main() { vector<string> names; }
12
The using keyword When we declare using namespace std; we are bringing the namespace std into the current scope so we don’t have to type std::string, std::cout, etc. all the time
13
When it is not included Compiler Error!
#include <vector> #include <string> int main() { vector<string> names; } Compiler Error!
14
We have to prepend to things from std library
#include <vector> #include <string> int main() { std::vector<std::string> names; }
15
Using namespace and scope resolution?
#include <vector> #include <string> using namespace std; int main() { std::vector<string> names; } Still Works!
16
‘using namespace’ multiple times
Allowed using namespace std; using namespace OtherLibrary; Problem??? What if std and OtherLibrary both have a ‘list’? Which one does list refer to?
17
Identifier Resolution
Check local scope (local variables and parameters) If member function, try pre-pending ‘this->’ and see if it is in the class Check ‘using’ declarations: For each namespace try pre-pending ‘namespaceName::’ What if multiple namespaces have a match? Not always the one you intended! Undefined (i.e. depends on the compiler) Therefore unpredictable. Use scope resolution to explicitly select the desired one.
18
Notes on Namespaces Commonly used in libraries (e.g. OpenCV, FLTK, etc.) You probably won’t need to create your own namespaces any time soon Some argue that you should never utilize ‘using namespace std;’ Instead always use scope resolution (e.g. std::) Benefit is never having ambiguity.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.