C++程序语言设计.ppt

上传人:周芸 文档编号:379232 上传时间:2018-10-09 格式:PPT 页数:37 大小:176KB
下载 相关 举报
C++程序语言设计.ppt_第1页
第1页 / 共37页
C++程序语言设计.ppt_第2页
第2页 / 共37页
C++程序语言设计.ppt_第3页
第3页 / 共37页
C++程序语言设计.ppt_第4页
第4页 / 共37页
C++程序语言设计.ppt_第5页
第5页 / 共37页
亲,该文档总共37页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

1、C+程序语言设计,Chapter 9: Name Control,Outline,How to control storage and visibility by the static keywordC+s namespace featureC+s References feature,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 visib

2、ility of a name Local to a particular translation unit can not be seen outside the translation unit or class,Static variables inside functions,How to retain a value between function calls? making a global variable create a static object inside a functionThis object is initialized only once, the firs

3、t time the function is called, and then retains its value between function invocations. see the example StaticVarInFunc.cpp,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 wit

4、h constructor calls if you dont specify constructor arguments, the class must have a default constructor. see the example StaticObjInFunc.cpp,Static object destructors,Destructors for static objects are called when main( ) exits when the Standard C library function exit( ) is explicitly called Destr

5、uction of static objects occurs in the reverse order of initialization only objects that have been constructed are destroyed see the example StaticDestructors.cpp,Controlling linkage,external linkage Any name at file scope is visible throughout all translation units in a program Global variables and

6、 ordinary functions have external linkage,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.,cross over each other,At file scope int a =

7、0; extern int a = 0; store in the programs static data area external linkage : the visibility is global across all translation units static int a = 0; store in the programs static data area internal linkage : the visibility is local to its translation unit.,cross over each other,Once get into local

8、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.,Class static members,How to a shared storage spa

9、ce 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,Class static members,accomplished with static data members inside a class There is a single piece of st

10、orage for a static data member All objects share the same static storage space the static datas name is scoped inside the class Can be public, private, or protected,Define storage for static data member,The definition must occur outside the class only one definition is allowed it is common to put it

11、 in the implementation file for the class see the example StaticInit.cpp,Define storage for static data member,Doesnt 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

12、the class creator is forced to create the definition This ensures that the definition happens only once and that its in the hands of the class creator.,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 f

13、unction 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,static member functions,A static member function cannot access ordinary data members, only static data members.

14、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,Access of Class static members,public static members can be accessed by any object of class private static members can be accessed b

15、y 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,Namespaces,In a large project, lack of control over the global name space can cause probl

16、ems. To subdivide the global name space into more manageable pieces using the namespace feature of C+.,Creating a namespace,Similar to the creation of a class namespace MyLib /Declarations int main() ,Differences from class,appear only at global scope nested within another namespace no terminating s

17、emicolon is necessary be “continued” over multiple header files using a syntax be aliased to another name cannot create an instance,Unnamed namespaces,Each translation unit contains an unnamed namespace that you can add to by saying “namespace” without an identifier automatically available in that t

18、ranslation unit without qualification be unique for each translation unit see the example UnnamedNS.cpp,Friends,Inject a friend declaration into a namespace by declaring it within an enclosed classsee the example FriendInjection.cpp,Using a namespace,Refer to a name within a namespace in three ways

19、: by specifying the name using the scope resolution operator with a using directive to introduce all names in the namespace with a using declaration to introduce names one at a time,Scope resolution,Any name in a namespace can be explicitly specified using the scope resolution operator see the examp

20、le ScopeResolution.cpp,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,The using directive,One aspect of the using directive may seem

21、slightly counterintuitive at first. override the names from the using directive as if theyve been declared globally to that scope! see the example OverridingAmbiguity.cpp,The using declaration,inject names one at a time into the current scope with a using declaration can override names from a using

22、directive see the example UsingDeclaration1.cpp,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,References,References are

23、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.“复姓诸葛,名亮,字孔明,号卧龙先生。”,Pointers in C+,C+ is a more strongly typed language. C allows you to casually assign a pointer of one type to

24、another through a void* bird* b; rock* r; void* v; v = r; b = v; In C+, make it explicit using a cast,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,Refe

25、rences in C+,It is usually used for: function argument lists function return valuessee the example funcArgument.cpp 参见引用作为返回值的一些规则 ,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 ca

26、nnot 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. 参见引用和指针的比较,References in functions,used as a function argument any modification to the reference inside the f

27、unction will cause changes to the argument outside the function return a reference from a function Whatever the reference is connected to shouldnt go away when the function returns, otherwise youll be referring to unknown memory see the example Reference.cpp,const references,if you know the function

28、 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 wont modify any public data members Temporary objects are always const see th

29、e example ConstReferenceArguments.cpp,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.cppthe pointer is incremented, not what it points to,thank you!,next lesson The copy-constructor,

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 教学课件 > 大学教育

copyright@ 2008-2019 麦多课文库(www.mydoc123.com)网站版权所有
备案/许可证编号:苏ICP备17064731号-1