COM262 Object Development - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

COM262 Object Development

Description:

C solution: make the names different (Library1String and Library2String) ... enum { Server = 128, Username = 129 } namespace NewsPreferences ... – PowerPoint PPT presentation

Number of Views:19
Avg rating:3.0/5.0
Slides: 23
Provided by: gileso
Category:

less

Transcript and Presenter's Notes

Title: COM262 Object Development


1
COM262 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
  • Both PublicFunction() and PrivateFunction() are
    visible outside the class, as ExamplePublicFunct
    ion() and ExamplePrivateFunction()

class Example public void PublicFunction
() private void PrivateFunction ()
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
Dummy 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

5
Dummy classes..
  • class Library1
  • public
  • static void Function1 ()
  • static void Function2 ()
  • class Library2
  • public
  • static void Function1 (int)
  • static void Function2 (char)

6
Namespace 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
7
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
8
Using a namespace
  • namespace Library1
  • class String
  • class List
  • Explicit qualification
  • void DoSomething ()
  • Library1String string
  • Library1List list
  • // ...

9
Using a namespace
  • using declaration
  • void DoSomething ()
  • using Library1String
  • using Library1List
  • String string
  • List list
  • // ...

10
Using a namespace
  • using directive
  • void DoSomething ()
  • using namespace Library1
  • String string
  • List list
  • or any combination...

11
Resolving conflicts
namespace Library1 class String class
List namespace Library2 class String
class List
12
Resolving conflicts
  • Explicit qualification
  • void DoSomething ()
  • Library1String string1
  • Library2String string2
  • // ...
  • Using declaration
  • void DoSomething ()
  • using Library1String
  • // Really Library1String
  • String string
  • Library2String string
  • // ...

13
Using a namespace
namespace Library1 namespace Part1
class String class List
namespace Part2 class String
class List class Array
14
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
15
Namespace aliasing
void DoSomething () namespace LP2
LibraryPart2 // Really LibraryPart2String
LP2String string
16
Anonymous 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
17
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)

18
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
// ...
19
Namespaces 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)
20
Questions 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
21
Questions 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.

22
Questions 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
Write a Comment
User Comments (0)
About PowerShow.com