Download presentation
Presentation is loading. Please wait.
Published byTiffany Jacobs Modified over 9 years ago
1
C++ 程序语言设计 Chapter 9: Name Control
2
Outline How to control storage and visibility by the static keyword C++’s namespace feature C++’s References feature
3
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
4
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
5
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
6
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
7
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
8
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.
9
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.
10
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.
11
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
12
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
13
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
14
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.
15
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
16
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
17
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 operator see the example – StaticEmployee.cpp
18
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++.
19
Creating a namespace Similar to the creation of a class namespace MyLib { //Declarations } int main() { }
20
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
21
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
22
Friends Inject a friend declaration into a namespace by declaring it within an enclosed class see the example – FriendInjection.cpp
23
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
24
Scope resolution Any name in a namespace can be explicitly specified using the scope resolution operator see the example – ScopeResolution.cpp
25
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
26
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
27
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
28
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
29
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. “ 复姓诸葛,名亮,字孔明,号卧龙先生。 ”
30
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
31
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
32
References in C++ It is usually used for: function argument lists function return values see the example – funcArgument.cpp 参见《引用作为返回值的一些规则 》
33
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. 参见《引用和指针的比较》
34
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
35
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
36
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
37
thank you! next lesson – The copy-constructor
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.