Download presentation
Presentation is loading. Please wait.
1
CS212: Object Oriented Analysis and Design
Namespace
2
Introduction Purpose is to localize the names of identifiers to avoid name collisions Global namespace Name defined by one library would conflict with the same name defined by the other library Namespace allows the same name to be used in different contexts without conflicts arising e.g. std
3
Namespace Fundamentals
Allows you to partition the global namespace Creating a declarative region i.e. a namespace defines a scope Inside a namespace, identifiers declared within that namespace can be referred to directly Scope resolution operator to be used to refer to objects declared within a namespace from outside that namespace Demonstration: namespace1.cpp
4
The ‘using’ keyword Use of :: always is a tedious task
The using statement was invented to alleviate this problem The using statement has these two general forms using namespace name; using name::member; using CounterNameSpace::lowerbound; Demonstration: namespace2.cpp
5
Object Oriented Analysis and Design (CS 212)
Nested Namespace Namespace defined within another namespace Scope resolution operator to be used Namespace1::Namespace2::member Namespace1::member Object Oriented Analysis and Design (CS 212)
6
Unnamed Namespaces A special type of namespace, called an unnamed namespace Allows to create identifiers that are unique within a file To establish unique identifiers that are known only within the scope of a single file Outside the file, the identifiers are unknown Demonstration (file1 & file2.cpp)
7
Object Oriented Analysis and Design (CS 212)
Namespace Alias Alias (alternate name) can be assigned to a namespace Ease of programming Similar to typedef Object Oriented Analysis and Design (CS 212)
8
Object Oriented Analysis and Design (CS 212)
Koening Lookup Argument-dependent lookup, also known as ADL, or Koenig lookup Set of rules for looking up the unqualified function names function-call expressions, including implicit function calls to overloaded operators These function names are looked up in The namespaces of their arguments The scopes and namespaces considered by the usual unqualified name lookup. Object Oriented Analysis and Design (CS 212)
9
Advantage of Koening Lookup
myLine l1(point(2,5), point(9,10)); std::cout << “equation of line is: ”; std::cout << l1 << end; Object Oriented Analysis and Design (CS 212)
10
In a nutshell Standard namespace Variables Functions Classes
User defined namespace Variables Functions Classes Variables Functions Classes
11
The std namespace Standard C++ defines its entire library in its own namespace called std using namespace std; This causes the std namespace to be brought into the current namespace Direct access to the names of the functions and classes defined within the library without using std::
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.