C++关键字及说明解释.doc_第1页
C++关键字及说明解释.doc_第2页
C++关键字及说明解释.doc_第3页
C++关键字及说明解释.doc_第4页
C++关键字及说明解释.doc_第5页
已阅读5页,还剩15页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

C+关键词asm auto bad_cast bad_typeid bool break case catch char class const const_cast continue default delete do double dynamic_cast else enum except explicit extern false finally float for friend goto if inline int long mutable namespace new operator private protected public register reinterpret_cast return short signed sizeof static static_cast struct switch template this throw true try type_info typedef typeid typename union unsigned using virtual void volatile wchar_t while (1)asmasm已经被_asm替代了,用于汇编语言嵌入在C/C+程序里编程,从而在某些方面优化代码.虽然用asm关键词编译时编译器不会报错,但是asm模块的代码是没有意义的.(2)auto 这个这个关键词用于声明变量的生存期为自动,即将不在任何类、结构、枚举、联合和函数中定义的变量视为全局变量,而在函数中定义的变量视为局部变量。这个关键词不怎么多写,因为所有的变量默认就是auto的。(3)bad_cast,const_cast,dynamic_cast,reinterpret_cast,static_cast关于异常处理的,还不是太了解.(4)bad_typeid也是用于异常处理的,当typeid操作符的操作数typeid为Null指针时抛出.(5)bool不用多说了吧,声明布尔类型的变量或函数.(6)break跳出当前循环.The break statement terminates the execution of the nearest enclosing loop or conditional statement in which it appears. (7)caseswitch语句分支.Labels that appear after the case keyword cannot also appear outside a switch statement. (8)catch,throw,try都是异常处理的语句,The try, throw, and catch statements implement exception handling.(9)char声明字符型变量或函数.(10)class声明或定义类或者类的对象.The class keyword declares a class type or defines an object of a class type.(11)const被const修饰的东西都受到强制保护,可以预防意外的变动,能提高程序的健壮性。它可以修饰函数的参数、返回值,甚至函数的定义体。 作用: 1.修饰输入参数 a.对于非内部数据类型的输入参数,应该将“值传递”的方式改为“const引用传递”,目的是提高效率。例如将void Func(A a) 改为void Func(const A &a)。 b.对于内部数据类型的输入参数,不要将“值传递”的方式改为“const引用传递”。否则既达不到提高效率的目的,又降低了函数的可理解性。例如void Func(int x) 不应该改为void Func(const int &x)。 2.用const修饰函数的返回值 a.如果给以“指针传递”方式的函数返回值加const修饰,那么函数返回值(即指针)的内容不能被修改,该返回值只能被赋给加const修饰的同类型指针。 如对于:const char * GetString(void); 如下语句将出现编译错误: char *str = GetString();/cannot convert from const char * to char *; 正确的用法是: const char *str = GetString(); b.如果函数返回值采用“值传递方式”,由于函数会把返回值复制到外部临时的存储单元中,加const修饰没有任何价值。 如不要把函数int GetInt(void) 写成const int GetInt(void)。 3.const成员函数的声明中,const关键词只能放在函数声明的尾部,表示该类成员不修改对象. 说明: const type m; /修饰m为不可改变 示例: typedef char * pStr; /新的类型pStr; char string4 = abc; const char *p1 = string; p1+; /正确,上边修饰的是*p1,p1可变 const pStr p2 = string; p2+; /错误,上边修饰的是p2,p2不可变,*p2可变 同理,const修饰指针时用此原则判断就不会混淆了。 const int *value; /*value不可变,value可变 int* const value; /value不可变,*value可变 const (int *) value; /(int *)是一种type,value不可变,*value可变 /逻辑上这样理解,编译不能通过,需要tydef int* NewType; const int* const value;/*value,value都不可变(12)continue结束当前循环,开始下一轮循环.Forces transfer of control to the controlling expression of the smallest enclosing do, for, or while loop. (13)defaultswitch语句中的默认分支.None of the constants match the constants in the case labels; a default label is present.Control is transferred to the default label.(14)delete经常用于动态内存分配的语句,Deallocates a block of memory. (15)do在do-while循环结构中开始循环体.Executes a statement repeatedly until the specified termination condition (the expression) evaluates to zero.(16)double声明双精度变量或函数.(17)else条件语句否定分支(与 if 连用).(18)enum声明枚举类型.The name of each enumerator is treated as a constant and must be unique within the scope where the enum is defined. (19)explicitexplicit主要用于 修饰 构造函数,使得它不用于程序中需要通过此构造函数进行 隐式 转换的情况,防止不必要的隐式转化.;指定此关键字,需要隐式转换方可进行的程序将会不能通过. 而可通过强制转换使它没有用.This keyword is a declaration specifier that can only be applied to in-class constructor declarations. An explicit constructor cannot take part in implicit conversions. It can only be used to explicitly construct an object. (20)exportMSDN只说The export keyword is not supported on templates.一种导出语句吧.(21)externextern 意为“外来的”它的作用在于告诉编译器:有这个变量,它可能不存在当前的檔中,但它肯定要存在于工程中的某一个源文件中或者一个Dll的输出中。声明变量是在其它文件中声明(也可以看做是引用变量).Objects and variables declared as extern declare an object that is defined in another translation unit or in an enclosing scope as having external linkage.(22)false, truebool类型的两个枚举值.(23)float声明浮点型变量或函数.(24)for一种循环语句(可意会不可言传).Use the for statement to construct loops that must execute a specified number of times.(25)friend声明友元函数或者类.The friend keyword allows a function or class to gain access to the private and protected members of a class. (26)goto无条件跳转语句.Performs an unconditional transfer of control to the named label. (27)if条件语句.Controls conditional branching.常与else一起用.(28)inline声明定义内联函数,编译时将所调用的代码嵌入到主函数中.The inline specifiers instruct the compiler to insert a copy of the function body into each place the function is called.(29)int声明整型变量或函数.(30)long声明长整型变量或函数.(31)mutablemutalbe的中文意思是“可变的,易变的”,跟constant(既C+中的const)是反义词。在C+中,mutable也是为了突破const的限制而设置的。被mutable修饰的变量,将永远处于可变的状态,即使在一个const函数中。如果类的成员函数不会改变对象的状态,那么这个成员函数一般会声明成const的。但是,有些时候,我们需要在const的函数里面修改一些跟类状态无关的数据成员,那么这个数据成员就应该被mutalbe来修饰。This keyword can only be applied to non-static and non-const data members of a class. If a data member is declared mutable, then it is legal to assign a value to this data member from a const member function.(32) namespace如同名字一样的意思,NameSpace:名字空间,之所以出来这样一个东西,是因为人类可用的单词数太少,并且不同的人写的程序不可能所有的变量都没有重名现象,对于库来说,这个问题尤其严重,如果两个人写的库文件中出现同名的变量或函数(不可避免),使用起来就有问题了,为了解决这个问题,引入了名字空间这个概念,通过使用 namespace xxx;你所使用的库函数或变量就是在该名字空间中定义的.这样一来就不会引起不必要的冲突了.命名空间是用来组织和重用代码的编译单元。所谓namespace,是指标识符的各种可见范围。C+标准程序库中的所有标识符都被定义于一个名为std的namespace中。 一 :和格式不一样,前者没有后缀,实际上,在你的编译器include文件夹里面可以看到,二者是两个文件,打开文件就会发现,里面的代码是不一样的。后缀为.h的头文件c+标准已经明确提出不支持了,早些的实现将标准库功能定义在全局空间里,声明在带.h后缀的头文件里,c+标准为了和C区别开,也为了正确使用命名空间,规定头文件不使用后缀.h。 因此,当使用时,相当于在c中调用库函数,使用的是全局命名空间,也就是早期的c+实现;当使用的时候,该头文件没有定义全局命名空间,必须使用namespace std;这样才能正确使用cout。 二: 所谓namespace,是指标识符的各种可见范围。 C+标准程序库中的所有标识符都被定义于一个名为std的namespace中。 由于namespace的概念,使用C+标准程序库的任何标识符时,可以有三种选择: 1、直接指定标识符。例如std:ostream而不是ostream。完整语句如下: std:cout std:hex 3.4 std:endl; 2、使用using关键字。 using std:cout; using std:endl; using std:cin; 以上程序可以写成 cout std:hex 3.4 endl; 3、最方便的就是使用using namespace std; 例如: #include#include #include using namespace std;这样命名空间std内定义的所有标识符都有效(曝光)。就好像它们被声明为全局变量一样。那么以上语句可以如下写: cout hex 3.4 endl;因为标准库非常的庞大,所以程序员在选择的类的名称或函数名时就很有可能和标准库中的某个名字相同。所以为了避免这种情况所造成的名字冲突,就把标准库中的一切都被放在名字空间std中。但这又会带来了一个新问题。无数原有的C+代码都依赖于使用了多年的伪标准库中的功能,他们都是在全局空间下的。所以就有了和等等这样的头文件,一个是为了兼容以前的C+代码,一个是为了支持新的标准。命名空间std封装的是标准程序库的名称,标准程序库为了和以前的头文件区别,一般不加.hDynamically imports an element behavior into a document.(33)new动态内存分配.Allocates memory for an object or array of objects of type-name from the free store and returns a suitably typed, nonzero pointer to the object.(34) operator它和运算符一起使用,表示一个运算符函数,理解时应将operator=整体上视为一个函数名。这是C+扩展运算符功能的方法,虽然样子古怪,但也可以理解:一方面要使运算符的使用方法与其原来一致,另一方面扩展其功能只能通过函数的方式(c+中,“功能”都是由函数实现的)。The operator keyword declares a function specifying what operator-symbol means when applied to instances of a class. (35)private类私有函数和数据成员的标示.When preceding a list of class members, the private keyword specifies that those members are accessible only from member functions and friends of the class. This applies to all members declared up to the next access specifier or the end of the class.(36)protected具有protected访问控制级别的成员是半公开的,外界无法直接访问这个控制级别的成员,但是派生类的base指针可以获得访问能力。The protected keyword specifies access to class members in the member-list up to the next access specifier (public or private) or the end of the class definition. (37)publicpublic在程序语言中基本都表示全局变量或者全局函数,他的本意是“公共的”的意思,他表示某个变量或者函数是全局函数When preceding a list of class members, the public keyword specifies that those members are accessible from any function. This applies to all members declared up to the next access specifier or the end of the class.(38)register 声明积存器变数.The register keyword specifies that the variable is to be stored in a machine register, if possible.这个关键词命令编译器尽可能的将变量存在CPU内部寄存器中,而不是通过内存寻址访问,从而提高效率。 register修饰符暗示编译程序相应的变量将被频繁地使用,如果可能的话,应将其保存在CPU的寄存器中,以加快其存储速度。首先,register变量必须是能被CPU所接受的类型。这通常意味着register变量必须是一个单个的值,并且长度应该小于或者等于整型的长度。不过,有些机器的寄存器也能存放浮点数。 其次,因为register变量可能不存放在内存中,所以不能用“&”来获取register变量的地址。 由于寄存器的数量有限,而且某些寄存器只能接受特定类型的数据(如指针和浮点数),因此真正起作用的register修饰符的数目和类型都依赖于运行程序的机器,而任何多余的register修饰符都将被编译程序所忽略。 在某些情况下,把变量保存在寄存器中反而会降低程序的运行速度。因为被占用的寄存器不能再用于其它目的;或者变量被使用的次数不够多,不足以装入和存储变量所带来的额外开销。(39)return子程序返回语句(可以带参数,也看不带参数),返回函数调用点.Terminates the execution of a function and returns control to the calling function (or, in the case of the main function, transfers control back to the operating system). Execution resumes in the calling function at the point immediately following the call.(40)short声明短整型变量或函数.(41)signed, unsigned声明有符号类型变量或函数;声明无符号类型变量或函数.(42)static声明静态变量.When modifying a variable, the static keyword specifies that the variable has static durationinitializes it to 0 unless another value is specified.(43)struct声明结构体变量或函数.struct 类型是一种值类型,通常用来封装小型相关变量组.(44)switch选择Allows selection among multiple sections of code, depending on the value of an integral expression.(45)template模板template是一个声明模板的关键字,表示声明一个模板关键字class不能省略,如果类型形参多余一个 ,每个形参前都要加class 可以包含基本数据类型可以包含类类型.函数模板的一般形式如下:Template 模板定义:模板就是实现代码重用机制的一种工具,它可以实现类型参数化,即把类型定义为参数, 从而实现了真正的代码可重用性。模版可以分为两类,一个是函数模版,另外一个是类模版。 The template declaration specifies a set of parameterized classes or functions. (46)this一个对象的this指针并不是对象本身的一部分,不会影响sizeof(对象)的结果。this作用域是在类内部,当在类的非静态成员函数中访问类的非静态成员的时候,编译器会自动将对象本身的地址作为一个隐含参数传递给函数。也就是说,即使你没有写上this指针,编译器在编译的时候也是加上this的,它作为非静态成员函数的隐含形参,对各成员的访问均通过this进行。this指针存在于类的成员函数中,指向被调用函数所在的类实例的地址。一种情况就是,在类的非静态成员函数中返回类对象本身的时候,直接使用 return *this;另外一种情况是当参数与成员变量名相同时,如this-n = n (不能写成n = n)。The this pointer is a pointer accessible only within the nonstatic member functions of a class, struct, or union type. (47)typedef用以给数据类型取别名.Introduces a name that, within its scope, becomes a synonym for the type given by the type-declaration portion of the declaration.(48)typeidtypeid is used to get the Type for a type at compile time.标准C+的一个新特征是RTTI(Run-Time Type Information运行时类型信息),它为程序在运行时确定对象类型,提供了一种标准方法。在标准C+中,有三个支持RTTI的元素:1、关键字dynamic_cast(动态强制转换):操作符dynamic_cast将一个指向基类的指针转换为一个指向派生类的指针(如果不能正确转换,则返回0空指针),格式为dynamic_cast ( exdivssion )dynamic_cast在转化过程中要用到相关类的类型信息类type_info中的信息。2、关键字typeid(类型标识符):用来检测指针类型(返回type_info类对象的指针),格式为typeid ( exdivssion ) 或 typeid ( type-id) 其中,exdivssion为结果为对象的表达式,type-id为类名。3、类type_info(类型信息):存储特定类型的有关信息,定义在头文件中。type_info类的具体内容由编译器实现来决定,但是至少必须包含返回字符串的name()成员函数。(49)typenameTells the compiler that an unknown identifier is a type.Use this keyword only in template definitions.在下面的 template declarations(模板声明)中 class 和 typename 有什么不同?template class Widget; / uses classtemplate class Widget; / uses typename答案:没什么不同。在声明一个 template type parameter(模板类型参数)的时候,class 和 typename 意味着完全相同的东西。一些程序员更喜欢在所有的时间都用 class,因为它更容易输入。其他人(包括我本人)更喜欢 typename,因为它暗示着这个参数不必要是一个 class type(类类型)。少数开发者在任何类型都被允许的时候使用 typename,而把 class 保留给仅接受 user-defined types(用户定义类型)的场合。但是从 C+ 的观点看,class 和 typename 在声明一个 template parameter(模板参数)时意味着完全相同的东西。然而,C+ 并不总是把 class 和 typename 视为等同的东西。有时你必须使用 type

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论