Title: COM262 Object Development
1COM262 Object Development
2What 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
- Both PublicFunction() and PrivateFunction() are
visible outside the class, as ExamplePublicFunct
ion() and ExamplePrivateFunction()
class Example public void PublicFunction
() private void PrivateFunction ()
3Why 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)
4Dummy classes..
- Bad C solution use dummy classes to group
names - 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 -
5Dummy classes..
- class Library1
-
- public
- static void Function1 ()
- static void Function2 ()
-
- class Library2
-
- public
- static void Function1 (int)
- static void Function2 (char)
-
6Namespace syntax
- Very similar to class syntax, but
- No access specification (public, private,
protected) - No trailing semicolon
namespace MyNamespace void Function1 ()
void Function2 () typedef UInt32 MyInt32
MyInt32 gVariable
7Namespace syntax
- Namespaces are open -- they can be in several
independent header files
Library1String.h namespace Library1 class
String Library1List.h namespace Library1
class List
8Using a namespace
- namespace Library1
-
- class String
- class List
-
- Explicit qualification
- void DoSomething ()
-
- Library1String string
- Library1List list
- // ...
9Using a namespace
- using declaration
- void DoSomething ()
-
- using Library1String
- using Library1List
- String string
- List list
- // ...
-
10Using a namespace
- using directive
- void DoSomething ()
-
- using namespace Library1
- String string
- List list
-
- or any combination...
11Resolving conflicts
namespace Library1 class String class
List namespace Library2 class String
class List
12Resolving conflicts
- Explicit qualification
- void DoSomething ()
-
- Library1String string1
- Library2String string2
- // ...
-
- Using declaration
- void DoSomething ()
-
- using Library1String
- // Really Library1String
- String string
- Library2String string
- // ...
13Using a namespace
namespace Library1 namespace Part1
class String class List
namespace Part2 class String
class List class Array
14Combination 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
15Namespace aliasing
void DoSomething () namespace LP2
LibraryPart2 // Really LibraryPart2String
LP2String string
16Anonymous namespace
- 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
namespace Boolean gAllDone
ProcessSerialNumber gMyPSN
17std 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)
18Namespaces 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
// ...
19Namespaces in your application
void PreferencesDialogItemHit () switch
(itemHit) case MailPreferencesServer //
... case MailPreferencesUsername // ...
case NewsPreferencesServer // ... case
NewsPreferencesUsername // ... void
PreferencesDialogSetupMailPreferences ()
using MailPreferences SetValue (Server,
server) SetValue (Username, username)
20Questions 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
namespace n2 using n1c
21Questions and Answers
- 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.
22Questions 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