6 Namespaces
6 Namespaces
code modularization
Programming for Telecommunications
Filippo Campagnaro
Programming for TLC
[email protected]
Outline
1. Modularity
2. Namespaces
3. Access to namespaces
Programming for TLC
4. Interfaces
[c++pl] Chapter 14
Composition problem
• Any realistic program is composed by multiple,
separate parts – think of functions, classes, etc
• A good implementation should be based on
modularity
keep separate allow the access
things/concepts only through a
/abstractions well-specified
separate interface
Modularity
• using declarations
using std::string;
6
Access to namespace members
• using directives
• Don’t place them in the global scope of an header file (which could
be #included anywhere)
7
Access to namespace members
• with the argument-dependent lookup
• search for a function in the namespace of its
arguments
• it is particularly useful for operators
• the rules to find a function with the matching
signature are as follows:
• if the argument is a class member, first check the class,
then the namespace
• if the argument is a namespace member, first check the
namespace, then the (eventual) enclosing ones, up to
Namespaces
the global
• if the argument is built-in (int, char, bool, etc), there
are no associated namespaces 8
Modularization and interfaces
• A program is a combination of separate parts
• Each par needs access to the functionalities provided by
another part
• This can be done by defining interfaces
• They should be the only way to access the functionalities of a part
of code (a “module”)
• The implementation details should be hidden
• Data-hiding principle of the data abstraction programming
paradigm
• Interfaces can be defined with
• Namespaces (which could define libraries and modules)
Interfaces
9
Example of modular codebase
TCP module
Retransmissions
Retransmissions interface
implementation
Header parsing
Header parsing interface
implementation
Interfaces