Namespaces - PowerPoint PPT Presentation

About This Presentation
Title:

Namespaces

Description:

Qualified Names. A namespace is a scope. The usual scope rules ... The repeated qualification Lexer is tedious and distracting. Using Declarations Solution-1 ... – PowerPoint PPT presentation

Number of Views:36
Avg rating:3.0/5.0
Slides: 26
Provided by: cobyfer
Category:

less

Transcript and Presenter's Notes

Title: Namespaces


1
Namespaces
2
Calculator Case-study
  • Consider a simple desk calculator. It can be
    viewed as being composed of four parts
  • The lexer, composing tokens out of characters.
  • The parser, doing syntax analysis
  • The symbol table, holding (string, value) pairs.
  • The driver, main().

3
Modularization and Interfaces
Driver
Parser
Lexer
Symbol table
4
Namespaces
  • Ideally, every entity in a program belongs to
    some recognizable logical unit.
  • A namespace is a mechanism for expressing logical
    grouping.
  • A namespace separates the implementation from the
    interface.
  • The exception is main, which must be global in
    order for the run time environment to recognize
    it as special.

5
Calculator Parser
  • namespace Parser
  • double prim(bool)
  • double term(bool)
  • double expr(bool)
  • double Parserprim(bool get)
  • double Parserterm(bool get)
  • double Parserexpr(bool get)

6
Qualified Names
  • A namespace is a scope.
  • The usual scope rules hold for namespaces.
  • A name from another namespace can be used when
    qualified by the name of its namespace.

7
Qualified Names Example
  • double Parserterm(bool get) //qualification
    for function impl.
  • double left prim(get)
  • //no need for qualification same //namespace
  • for()
  • switch(Lexercurr_tok) case LexerMUL

8
Using Declarations
  • When a name is frequently used outside its
    namespace, it can be bother to repeatedly qualify
    it with its namespace name.

9
Using Declarations Problem
  • double Parserprim(bool get)
  • if(get) Lexerget_token()
  • switch(Lexercurr_tok)
  • case LexerNUMBER
  • Lexerget_token()
  • return Lexernumber_value
  • case LexerNAME
  • The repeated qualification Lexer is tedious and
    distracting.

10
Using Declarations Solution-1
  • double Parserprim(bool get)
  • using Lexerget_token
  • using Lexercur_tok
  • if(get) get_token()
  • switch(curr_tok)
  • case LexerNUMBER
  • get_token()
  • return Lexernumber_value
  • case LexerNAME
  • A using declaration introduces a local synonym.

11
Using Declarations Solution-2
  • namespace Parser
  • double prim(bool)
  • double term(bool)
  • double expr(bool)
  • using Lexerget_token
  • using Lexercur_tok

12
Using Declarations Solution-3
  • namespace Parser
  • double prim(bool)
  • double term(bool)
  • double expr(bool)
  • using namespace Lexer
  • A using directive makes names from a namespace
    available almost if they had been detached
    outside their namespace.

13
Multiple Interfaces
  • We can view every module used to provide two
    general things
  • The common environment for the function
    implementing the module.
  • The external interface offered be the module to
    its users.

14
Multiple Interfaces Example
Parser
Parser Implementation
Parser Interface
Driver
15
Multiple Interfaces Example Cont
  • namespace Parser
  • double prim(bool)
  • double term(bool)
  • double expr(bool)
  • namespace Parser_interface
  • using Parserexpr

16
Avoiding Name Clashes
  • Namespaces are intended to express logical
    structure. The simplest such structure is the
    distinction between code written by one person
    vs. code written by someone else.

17
Name Clashes Example
  • //my.h
  • char f(char)
  • int f(int)
  • class String
  • //your.h
  • char f(char)
  • class String
  • double f(double)

18
Name Clashes Solution
  • namespace My
  • char f(char)
  • int f(int)
  • class String
  • namespace Your
  • char f(char)
  • class String
  • double f(double)

19
Namespace Aliases
  • If we give short names to our namespaces, the
    names of different namespaces will clash.
  • Long namespaces names can be impractical in real
    code.
  • The dilemma can be resolved by providing a short
    alias for a longer namespace name.

20
Namespace Aliases Example
  • namespace Programming_Labratory_A
  • //too long
  • namespace PLAB Programming_Labratory_A

21
Namespace Composition and Selection
  • namespace His_String
  • namespace Her_vector
  • templateltclass Tgt class Vector
  • namespace My_lib
  • using namespace His_string
  • using namespace Her_vector
  • void my_fct(String)

22
Namespace Composition and Selection
  • namespace His_lib
  • class String
  • namespace Her_lib
  • class String
  • namespace My_lib
  • using namespace His_lib
  • using namespace Her_lib
  • using His_libString //resolve potential clash
  • templateltclasss Tgt class List //additional
    stuff

23
Namespaces and C
  • //stdio.h
  • namespace std
  • int printf(const char )
  • using namespace std
  • //cstdio
  • namespace std
  • int printf(const char )

24
Namespace Are Open
  • A namespace is open that is, you can add names
    to it form several namespaces declarations.
  • namespace A
  • int f()
  • namespace A
  • int g()

25
Summery
  • Use namespaces to express logical structure.
  • Design a namespace so that you can conveniently
    use it without accidentally gaining access to
    unrelated namespaces.
  • Avoid placing heavy notational burdens on users
    of your namespaces.
Write a Comment
User Comments (0)
About PowerShow.com