c++ 程序语言设计 chapter 9: name control. outline how to control storage and visibility by...

37
C++ 程程程程程程 Chapter 9: Name Control

Upload: tiffany-jacobs

Post on 05-Jan-2016

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: C++ 程序语言设计 Chapter 9: Name Control. Outline  How to control storage and visibility by the static keyword  C++’s namespace feature  C++’s References

C++ 程序语言设计

Chapter 9: Name Control

Page 2: C++ 程序语言设计 Chapter 9: Name Control. Outline  How to control storage and visibility by the static keyword  C++’s namespace feature  C++’s References

Outline How to control storage and visibility

by the static keyword

C++’s namespace feature

C++’s References feature

Page 3: C++ 程序语言设计 Chapter 9: Name Control. Outline  How to control storage and visibility by the static keyword  C++’s namespace feature  C++’s References

Two basic meanings of static

the concept of static storage allocated once at a fixed address created in a special static data area

static controls the visibility of a name Local to a particular translation unit can not be seen outside the translation

unit or class

Page 4: C++ 程序语言设计 Chapter 9: Name Control. Outline  How to control storage and visibility by the static keyword  C++’s namespace feature  C++’s References

Static variables inside functions

How to retain a value between function calls? making a global variable create a static object inside a function

This object is initialized only once, the first time the function is called, and then retains its value between function invocations.

see the example – StaticVarInFunc.cpp

Page 5: C++ 程序语言设计 Chapter 9: Name Control. Outline  How to control storage and visibility by the static keyword  C++’s namespace feature  C++’s References

static class objects inside functions

The rules are the same for static objects of user-defined types some initialization is required user-defined types must be initialized

with constructor calls if you don’t specify constructor

arguments, the class must have a default constructor.

see the example – StaticObjInFunc.cpp

Page 6: C++ 程序语言设计 Chapter 9: Name Control. Outline  How to control storage and visibility by the static keyword  C++’s namespace feature  C++’s References

Static object destructors

Destructors for static objects are called when main( ) exits when the Standard C library function

exit( ) is explicitly called Destruction of static objects occurs in

the reverse order of initialization only objects that have been constructed

are destroyed

see the example – StaticDestructors.cpp

Page 7: C++ 程序语言设计 Chapter 9: Name Control. Outline  How to control storage and visibility by the static keyword  C++’s namespace feature  C++’s References

Controlling linkage

external linkage Any name at file scope is visible

throughout all translation units in a program

Global variables and ordinary functions have external linkage

Page 8: C++ 程序语言设计 Chapter 9: Name Control. Outline  How to control storage and visibility by the static keyword  C++’s namespace feature  C++’s References

Controlling linkage

internal linkage An object or function name at file scope

that is explicitly declared static is local to its translation unit.

Use the same name in other translation units without a name clash.

Page 9: C++ 程序语言设计 Chapter 9: Name Control. Outline  How to control storage and visibility by the static keyword  C++’s namespace feature  C++’s References

cross over each other At file scope

int a = 0; extern int a = 0;

store in the program’s static data area external linkage : the visibility is global

across all translation units static int a = 0;

store in the program’s static data area internal linkage : the visibility is local to

its translation unit.

Page 10: C++ 程序语言设计 Chapter 9: Name Control. Outline  How to control storage and visibility by the static keyword  C++’s namespace feature  C++’s References

cross over each other

Once get into local variables, static stops altering the visibility and instead alters the storage class.

file static : With function names (for non-member

functions), static and extern can only alter visibility

static void f(); visible only within this translation unit.

Page 11: C++ 程序语言设计 Chapter 9: Name Control. Outline  How to control storage and visibility by the static keyword  C++’s namespace feature  C++’s References

Class static members

How to a shared storage space to be used by all objects of a class? for example : Class Hero, need 5 heros, heroCount the data could be stored as if it were

global be hidden inside a class clearly associated with that class

Page 12: C++ 程序语言设计 Chapter 9: Name Control. Outline  How to control storage and visibility by the static keyword  C++’s namespace feature  C++’s References

Class static members

accomplished with static data members inside a class There is a single piece of storage for a

static data member All objects share the same static storage

space the static data’s name is scoped inside

the class Can be public, private, or protected

Page 13: C++ 程序语言设计 Chapter 9: Name Control. Outline  How to control storage and visibility by the static keyword  C++’s namespace feature  C++’s References

Define storage for static data member

The definition must occur outside the class

only one definition is allowed it is common to put it in the

implementation file for the class

see the example – StaticInit.cpp

Page 14: C++ 程序语言设计 Chapter 9: Name Control. Outline  How to control storage and visibility by the static keyword  C++’s namespace feature  C++’s References

Define storage for static data member

Doesn’t this break the protection mechanism? the only place this initialization is legal is in the

definition once the definition has been made, the end-user

cannot make a second definition the class creator is forced to create the

definition This ensures that the definition happens only

once and that it’s in the hands of the class creator.

Page 15: C++ 程序语言设计 Chapter 9: Name Control. Outline  How to control storage and visibility by the static keyword  C++’s namespace feature  C++’s References

static member functions

static member functions work for the class as a whole rather than for a particular object of a class.

call a static member function in the ordinary way, with the dot or the

arrow, in association with an object without any specific object, using the

scope-resolution operator see the example – StaticMemberFunc.cpp

Page 16: C++ 程序语言设计 Chapter 9: Name Control. Outline  How to control storage and visibility by the static keyword  C++’s namespace feature  C++’s References

static member functions

A static member function cannot access ordinary data members, only static data members.

It can call only other static member functions

It can neither access non-static data members nor call non-static member functions see the example – StaticMemFuncCall.cpp

Page 17: C++ 程序语言设计 Chapter 9: Name Control. Outline  How to control storage and visibility by the static keyword  C++’s namespace feature  C++’s References

Access of Class static members

public static members can be accessed by any object of class

private static members can be accessed by using public member functions or friend of class

Class static members can be accessed even though none of object of class, using the class name and scope resolution operatorsee the example – StaticEmployee.cpp

Page 18: C++ 程序语言设计 Chapter 9: Name Control. Outline  How to control storage and visibility by the static keyword  C++’s namespace feature  C++’s References

Namespaces

In a large project, lack of control over the global name space can cause problems.

To subdivide the global name space into more manageable pieces using the namespace feature of C++.

Page 19: C++ 程序语言设计 Chapter 9: Name Control. Outline  How to control storage and visibility by the static keyword  C++’s namespace feature  C++’s References

Creating a namespace

Similar to the creation of a classnamespace MyLib{

//Declarations}int main() {}

Page 20: C++ 程序语言设计 Chapter 9: Name Control. Outline  How to control storage and visibility by the static keyword  C++’s namespace feature  C++’s References

Differences from class

appear only at global scope nested within another namespace no terminating semicolon is

necessary be “continued” over multiple header

files using a syntax be aliased to another name cannot create an instance

Page 21: C++ 程序语言设计 Chapter 9: Name Control. Outline  How to control storage and visibility by the static keyword  C++’s namespace feature  C++’s References

Unnamed namespaces

Each translation unit contains an unnamed namespace that you can add to by saying “namespace” without an identifier automatically available in that

translation unit without qualification be unique for each translation unit see the example – UnnamedNS.cpp

Page 22: C++ 程序语言设计 Chapter 9: Name Control. Outline  How to control storage and visibility by the static keyword  C++’s namespace feature  C++’s References

Friends

Inject a friend declaration into a namespace by declaring it within an enclosed class

see the example – FriendInjection.cpp

Page 23: C++ 程序语言设计 Chapter 9: Name Control. Outline  How to control storage and visibility by the static keyword  C++’s namespace feature  C++’s References

Using a namespace

Refer to a name within a namespace in three ways :1. by specifying the name using the scope

resolution operator 2. with a using directive to introduce all

names in the namespace 3. with a using declaration to introduce

names one at a time

Page 24: C++ 程序语言设计 Chapter 9: Name Control. Outline  How to control storage and visibility by the static keyword  C++’s namespace feature  C++’s References

Scope resolution

Any name in a namespace can be explicitly specified using the scope resolution operator

see the example – ScopeResolution.cpp

Page 25: C++ 程序语言设计 Chapter 9: Name Control. Outline  How to control storage and visibility by the static keyword  C++’s namespace feature  C++’s References

The using directive

When used in conjunction with the namespace keyword this is called a using directive

makes names appear as if they belong to the nearest enclosing namespace scope

see the example – Arithmetic.cpp

Page 26: C++ 程序语言设计 Chapter 9: Name Control. Outline  How to control storage and visibility by the static keyword  C++’s namespace feature  C++’s References

The using directive

One aspect of the using directive may seem slightly counterintuitive at first.

override the names from the using directive as if they’ve been declared globally to that scope!

see the example – OverridingAmbiguity.cpp

Page 27: C++ 程序语言设计 Chapter 9: Name Control. Outline  How to control storage and visibility by the static keyword  C++’s namespace feature  C++’s References

The using declaration

inject names one at a time into the current scope with a using declaration

can override names from a using directive see the example – UsingDeclaration1.cpp

Page 28: C++ 程序语言设计 Chapter 9: Name Control. Outline  How to control storage and visibility by the static keyword  C++’s namespace feature  C++’s References

The using declaration

The using declaration just gives the fully specified name of the identifier, but no type information.

put a using declaration anywhere a normal declaration can occur see the example – UsingDeclaration2.cpp

Page 29: C++ 程序语言设计 Chapter 9: Name Control. Outline  How to control storage and visibility by the static keyword  C++’s namespace feature  C++’s References

References

References are essential in C++ to support the syntax of operator overloading, but they are also a general convenience to control the way arguments are passed into and out of functions.

“ 复姓诸葛,名亮,字孔明,号卧龙先生。”

Page 30: C++ 程序语言设计 Chapter 9: Name Control. Outline  How to control storage and visibility by the static keyword  C++’s namespace feature  C++’s References

Pointers in C++

C++ is a more strongly typed language. C allows you to casually assign a pointer

of one type to another through a void*bird* b;rock* r;void* v;v = r;b = v;

In C++, make it explicit using a cast

Page 31: C++ 程序语言设计 Chapter 9: Name Control. Outline  How to control storage and visibility by the static keyword  C++’s namespace feature  C++’s References

References in C++ A reference (&) is like a constant

pointer that is automatically dereferenced.

you can make a free-standing reference.

see the example –FreeStandingReferences.cpp course09-01.cpp

Page 32: C++ 程序语言设计 Chapter 9: Name Control. Outline  How to control storage and visibility by the static keyword  C++’s namespace feature  C++’s References

References in C++

It is usually used for: function argument lists function return values

see the example – funcArgument.cpp参见《引用作为返回值的一些规则 》

Page 33: C++ 程序语言设计 Chapter 9: Name Control. Outline  How to control storage and visibility by the static keyword  C++’s namespace feature  C++’s References

References and Pointers A reference must be initialized when it is

created. Pointers can be initialized at any time. Once a reference is initialized to an object, it

cannot be changed to refer to another object. Pointers can be pointed to another object at any

time. You cannot have NULL references. a

reference is connected to a legitimate piece of storage.

参见《引用和指针的比较》

Page 34: C++ 程序语言设计 Chapter 9: Name Control. Outline  How to control storage and visibility by the static keyword  C++’s namespace feature  C++’s References

References in functions

used as a function argument any modification to the reference inside the function will cause changes to the argument outside the function

return a reference from a function Whatever the reference is connected to shouldn’t go away when the function returns, otherwise you’ll be referring to unknown memory

see the example – Reference.cpp

Page 35: C++ 程序语言设计 Chapter 9: Name Control. Outline  How to control storage and visibility by the static keyword  C++’s namespace feature  C++’s References

const references if you know the function will respect the

constness of an object, making the argument a const reference. for built-in types, the function will not modify the

argument for user-defined types, the function will call only

const member functions, and won’t modify any public data members

Temporary objects are always const see the example – ConstReferenceArguments.cpp

Page 36: C++ 程序语言设计 Chapter 9: Name Control. Outline  How to control storage and visibility by the static keyword  C++’s namespace feature  C++’s References

Pointer references to modify the contents of the pointer

rather than what it points to, the function argument becomes a reference to a pointer.

see the example – ReferenceToPointer.cpp

the pointer is incremented, not what it points to

Page 37: C++ 程序语言设计 Chapter 9: Name Control. Outline  How to control storage and visibility by the static keyword  C++’s namespace feature  C++’s References

thank you!

next lesson –

The copy-constructor