Title: COM262 OBJECT DEVELOPMENT
1COM262 OBJECT DEVELOPMENT
NAMESPACES
2- What is a namespace?
- Namespace a context in which we can declare and
define variables, functions, etc. - Names in a namespace are visible outside the
namespace - Example a class is a namespace
- class Example
-
- public
- void PublicFunction ()
- private
- void PrivateFunction ()
-
- Both PublicFunction() and PrivateFunction() are
visible outside the class, as ExamplePublicFunct
ion() and ExamplePrivateFunction().
3- Why do we need namespaces?
- Need to distinguish between different items with
the same name - Example two libraries both have a String class
- C solution make the names different
(Library1String and Library2String) - Requires vendors to cooperate, and makes all
names longer (kThemeWidgetCloseBox)
4- Bad C solution use dummy classes to group
names - class Library1
-
- public
- static void Function1 ()
- static void Function2 ()
-
- class Library2
-
- public
- static void Function1 (int)
- static void Function2 (char)
-
- Requires the entire library to be in one header
file (or included from it) - Everything not in a class or in a function had to
be global
5- Namespace syntax
- namespace MyNamespace
-
- void Function1 ()
- void Function2 ()
- typedef UInt32 MyInt32
- MyInt32 gVariable
-
- Very similar to class syntax, but
- No access specification (public, private,
protected) - No trailing semicolon
6- Namespace syntax
- Namespaces are open -- they can be in several
independent header files - Library1String.h
- namespace Library1
-
- class String
-
- Library1List.h
- namespace Library1
-
- class List
7- Using a namespace
- namespace Library1
-
- class String
- class List
-
- Explicit qualification
- void DoSomething ()
-
- Library1String string
- Library1List list
- // ...
-
8- Using a namespace
- using declaration
- void DoSomething ()
-
- using Library1String
- using Library1List
- String string
- List list
- // ...
-
9- Using a namespace
- using directive
- void DoSomething ()
-
- using namespace Library1
- String string
- List list
-
- or any combination...
10- Resolving conflicts
- namespace Library1
-
- class String
- class List
-
- namespace Library2
-
- class String
- class List
-
11- Explicit qualification
- void DoSomething ()
-
- Library1String string1
- Library2String string2
- // ...
-
- using declaration
- void DoSomething ()
-
- using Library1String
- // Really Library1String
- String string
- Library2String string
- // ...
-
12Nested namespaces namespace Library1
namespace Part1 class String class
List namespace Part2 class String
class List class Array
13- Use any combination of access techniques
- void DoSomething ()
-
- Library1Part1String string1
- Library1Part2String string2
- using Library1Part1String
- String string3
- using namespace Library1Part1
- // Really Library1Part1List List list1
- using namespace Library1
- // Really Library2Part2List
- Part2List list2
- // Really LibraryPart2
- using namespace Part2
- // Really LibraryPart2Array
- Array array
-
14- Namespace aliasing
- void DoSomething ()
-
- namespace LP2 LibraryPart2
- // Really LibraryPart2String
- LP2String string
-
15- The anonymous namespace
- namespace
-
- Boolean gAllDone
- ProcessSerialNumber gMyPSN
-
- The anonymous namespace is not accessible outside
the compilation unit - Replaces static for functions and variables
shared within one source file - Avoids using static to mean two completely
different things
16- The std namespace
- std namespace contains the entire C standard
library - Old-style headers (vector.h, string.h, etc) still
put everything in the global namespace - Better to use new-style headers (vector, string,
etc) - For C library, new-style headers start with "c"
(cstdio, cstdlib, etc)
17- Namespaces in your application
- Use namespaces instead of long names
- You can always shorted the names if necessary
with aliases and using directives and
declarations - class PreferencesDialog
-
- namespace MailPreferences
-
- enum Server 128, Username 129
-
- namespace NewsPreferences
-
- enum Server 131, Username 132
-
- // ...
18void PreferencesDialogItemHit () switch
(itemHit) case MailPreferencesServer //
... case MailPreferencesUsername // ...
case NewsPreferencesServer // ... case
NewsPreferencesUsername // ... void
PreferencesDialogSetupMailPreferences ()
using MailPreferences SetValue (Server,
server) SetValue (Username, username)
19Questions and Answers Q If I use a using
directive to "import" x into namespace y, do I
get two copies of x, or two names for one thing?
A Two names for one thing. Consider namespace
n1 class c public static int x
20namespace n2 using n1c In this case,
there is only one static variable created. It
can be accessed as either n1cx or n2cx,
but it is one variable. In other words,
n1cx n2cx.
21Questions and Answers Q When I use a using
declaration, what is its scope? A Same as any
other declaration. Consider void foo ()
using nc1 using nc2 c1 x1 //
OK, really nc1 c2 x2 // OK, really nc2
c1 x1 // OK, really nc1 c2 x2 //
error, c2 is not in this scope