C++程序语言设计.ppt
《C++程序语言设计.ppt》由会员分享,可在线阅读,更多相关《C++程序语言设计.ppt(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
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
2000 积分 0人已下载
下载 | 加入VIP,交流精品资源 |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 程序语言 设计 PPT
