ImageVerifierCode 换一换
格式:PPT , 页数:37 ,大小:176KB ,
资源ID:379232      下载积分:2000 积分
快捷下载
登录下载
邮箱/手机:
温馨提示:
如需开发票,请勿充值!快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。
如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝扫码支付 微信扫码支付   
注意:如需开发票,请勿充值!
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【http://www.mydoc123.com/d-379232.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(C++程序语言设计.ppt)为本站会员(周芸)主动上传,麦多课文库仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知麦多课文库(发送邮件至master@mydoc123.com或直接QQ联系客服),我们立即给予删除!

C++程序语言设计.ppt

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