C++关键字.docx

上传人:b****8 文档编号:9897702 上传时间:2023-02-07 格式:DOCX 页数:8 大小:19.56KB
下载 相关 举报
C++关键字.docx_第1页
第1页 / 共8页
C++关键字.docx_第2页
第2页 / 共8页
C++关键字.docx_第3页
第3页 / 共8页
C++关键字.docx_第4页
第4页 / 共8页
C++关键字.docx_第5页
第5页 / 共8页
点击查看更多>>
下载资源
资源描述

C++关键字.docx

《C++关键字.docx》由会员分享,可在线阅读,更多相关《C++关键字.docx(8页珍藏版)》请在冰豆网上搜索。

C++关键字.docx

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)asm

asm已经被__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

跳出当前循环.Thebreakstatementterminatestheexecutionofthenearestenclosinglooporconditionalstatementinwhichitappears.

(7)case

switch语句分支.Labelsthatappearafterthecasekeywordcannotalsoappearoutsideaswitchstatement.

(8)catch,throw,try

都是异常处理的语句,Thetry,throw,andcatchstatementsimplementexceptionhandling.

(9)char

声明字符型变量或函数.

(10)class

声明或定义类或者类的对象.Theclasskeyworddeclaresaclasstypeordefinesanobjectofaclasstype.

(11)const

被const修饰的东西都受到强制保护,可以预防意外的变动,能提高程序的健壮性。

它可以修饰函数的参数、返回值,甚至函数的定义体。

作用:

1.修饰输入参数

      a.对于非内部数据类型的输入参数,应该将“值传递”的方式改为“const引用传递”,目的是提高效率。

例如将voidFunc(Aa)改为voidFunc(constA&a)。

      b.对于内部数据类型的输入参数,不要将“值传递”的方式改为“const引用传递”。

否则既达不到提高效率的目的,又降低了函数的可理解性。

例如voidFunc(intx)不应该改为voidFunc(constint&x)。

2.用const修饰函数的返回值

      a.如果给以“指针传递”方式的函数返回值加const修饰,那么函数返回值(即指针)的内容不能被修改,该返回值只能被赋给加const修饰的同类型指针。

如对于:

constchar*GetString(void);

如下语句将出现编译错误:

char*str=GetString();//cannotconvertfrom'constchar*'to'char*';

正确的用法是:

constchar*str=GetString();

      b.如果函数返回值采用“值传递方式”,由于函数会把返回值复制到外部临时的存储单元中,加const修饰没有任何价值。

如不要把函数intGetInt(void)写成constintGetInt(void)。

3.const成员函数的声明中,const关键字只能放在函数声明的尾部,表示该类成员不修改对象.

说明:

consttypem;//修饰m为不可改变

示例:

typedefchar*pStr;//新的类型pStr;

charstring[4]="abc";

constchar*p1=string;

p1++;//正确,上边修饰的是*p1,p1可变

constpStrp2=string;

p2++;//错误,上边修饰的是p2,p2不可变,*p2可变

同理,const修饰指针时用此原则判断就不会混淆了。

constint*value;//*value不可变,value可变

int*constvalue;//value不可变,*value可变

const(int*)value;//(int*)是一种type,value不可变,*value可变

//逻辑上这样理解,编译不能通过,需要tydefint*NewType;

constint*constvalue;//*value,value都不可变

(12)continue

结束当前循环,开始下一轮循环.Forcestransferofcontroltothecontrollingexpressionofthesmallestenclosingdo,for,orwhileloop.

(13)default

switch语句中的默认分支.Noneoftheconstantsmatchtheconstantsinthecaselabels;adefaultlabelispresent.Controlistransferredtothedefaultlabel.

(14)delete

经常用于动态内存分配的语句,Deallocatesablockofmemory.

(15)do

在do-while循环结构中开始循环体.Executesastatementrepeatedlyuntilthespecifiedterminationcondition(theexpression)evaluatestozero.

(16)double

声明双精度变量或函数.

(17)else

条件语句否定分支(与if连用).

(18)enum

声明枚举类型.Thenameofeachenumeratoristreatedasaconstantandmustbeuniquewithinthescopewheretheenumisdefined.

(19)explicit

Thiskeywordisadeclarationspecifierthatcanonlybeappliedtoin-classconstructordeclarations.Anexplicitconstructorcannottakepartinimplicitconversions.Itcanonlybeusedtoexplicitlyconstructanobject.

(20)export

MSDN只说Theexportkeywordisnotsupportedontemplates.一种导出语句吧..

(21)extern

extern意为“外来的”···它的作用在于告诉编译器:

有这个变量,它可能不存在当前的文件中,但它肯定要存在于工程中的某一个源文件中或者一个Dll的输出中。

声明变量是在其他文件中声明(也可以看做是引用变量).Objectsandvariablesdeclaredasexterndeclareanobjectthatisdefinedinanothertranslationunitorinanenclosingscopeashavingexternallinkage.

(22)false,true

bool类型的两个枚举值.

(23)float

声明浮点型变量或函数.

(24)for

一种循环语句(可意会不可言传).Usetheforstatementtoconstructloopsthatmustexecuteaspecifiednumberoftimes.

(25)friend

声明友元函数或者类.Thefriendkeywordallowsafunctionorclasstogainaccesstotheprivateandprotectedmembersofaclass.

(26)goto

无条件跳转语句.Performsanunconditionaltransferofcontroltothenamedlabel.

(27)if

条件语句.Controlsconditionalbranching.常与else一起用.

(28)inline

声明定义内联函数,编译时将所调用的代码嵌入到主函数中.Theinlinespecifiersinstructthecompilertoinsertacopyofthefunctionbodyintoeachplacethefunctioniscalled.

(29)int

声明整型变量或函数.

(30)long

声明长整型变量或函数.

(31)mutable

Thiskeywordcanonlybeappliedtonon-staticandnon-constdatamembersofaclass.Ifadatamemberisdeclaredmutable,thenitislegaltoassignavaluetothisdatamemberfromaconstmemberfunction.

(32)namespace

Dynamicallyimportsanelementbehaviorintoadocument.

(33)new

动态内存分配.Allocatesmemoryforanobjectorarrayofobjectsoftype-namefromthefreestoreandreturnsasuitablytyped,nonzeropointertotheobject.

(34)operator

Theoperatorkeyworddeclaresafunctionspecifyingwhatoperator-symbolmeanswhenappliedtoinstancesofaclass.

(35)private

类私有函数和数据成员的标示.Whenprecedingalistofclassmembers,theprivatekeywordspecifiesthatthosemembersareaccessibleonlyfrommemberfunctionsandfriendsoftheclass.Thisappliestoallmembersdeclareduptothenextaccessspecifierortheendoftheclass.

(36)protected

Theprotectedkeywordspecifiesaccesstoclassmembersinthemember-listuptothenextaccessspecifier(publicorprivate)ortheendoftheclassdefinition.

(37)public

Whenprecedingalistofclassmembers,thepublickeywordspecifiesthatthosemembersareaccessiblefromanyfunction.Thisappliestoallmembersdeclareduptothenextaccessspecifierortheendoftheclass.

(38)register

声明积存器变量.Theregisterkeywordspecifiesthatthevariableistobestoredinamachineregister,ifpossible.这个关键字命令编译器尽可能的将变量存在CPU内部寄存器中,而不是通过内存寻址访问,从而提高效率。

(39)return

子程序返回语句(可以带参数,也看不带参数),返回函数调用点.Terminatestheexecutionofafunctionandreturnscontroltothecallingfunction(or,inthecaseofthemainfunction,transferscontrolbacktotheoperatingsystem).Executionresumesinthecallingfunctionatthepointimmediatelyfollowingthecall.

(40)short

声明短整型变量或函数.

(41)signed,unsigned

声明有符号类型变量或函数;声明无符号类型变量或函数.

(42)static

声明静态变量.Whenmodifyingavariable,thestatickeywordspecifiesthatthevariablehasstaticdurationinitializesitto0unlessanothervalueisspecified.

(43)struct

声明结构体变量或函数.struct类型是一种值类型,通常用来封装小型相关变量组.

(44)switch

Allowsselectionamongmultiplesectionsofcode,dependingonthevalueofanintegralexpression.

(45)template

模板.Thetemplatedeclarationspecifiesasetofparameterizedclassesorfunctions.

(46)this

Thethispointerisapointeraccessibleonlywithinthenonstaticmemberfunctionsofaclass,struct,oruniontype.

(47)typedef

用以给数据类型取别名.Introducesanamethat,withinitsscope,becomesasynonymforthetypegivenbythetype-declarationportionofthedeclaration.

(48)typeid

typeidisusedtogettheTypeforatypeatcompiletime.

(49)typename

Tellsthecompilerthatanunknownidentifierisatype.Usethiskeywordonlyintemplatedefinitions.

(50)union

声明联合数据类型.Aunionisauser-defineddataorclasstypethat,atanygiventime,containsonlyoneobjectfromitslistofmembers(althoughthatobjectcanbeanarrayoraclasstype).

(51)using

Theusingdeclarationintroducesanameintothedeclarativeregioninwhichtheusingdeclarationappears.

(52)virtual

声明虚基类或虚函数.Thevirtualkeyworddeclaresavirtualfunctionoravirtualbaseclass.

(53)void

声明函数无返回值或无参数,声明无类型指针.

Whenusedasafunctionreturntype,thevoidkeywordspecifiesthatthefunctiondoesnotreturnavalue.Whenusedforafunction'sparameterlist,voidspecifiesthatthefunctiontakesnoparameters.Whenusedinthedeclarationofapointer,voidspecifiesthatthepointeris"universal."

(54)volatile

说明变量在程序执行中可被隐含地改变,表明某个变量的值可能在外部被改变,优化器在用到这个变量时必须每次都小心地重新读取这个变量的值,而不是使用保存在寄存器里的备份。

Thevolatilekeywordisatypequalifierusedtodeclarethatanobjectcanbemodifiedintheprogrambysomethingsuchastheoperatingsystem,thehardware,oraconcurrentlyexecutingthread.

(55)wchar_t

宽字.

(56)while

循环语句的循环条件

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

当前位置:首页 > 工程科技 > 机械仪表

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1