12-3-13c++中关于const的应用

本文详细解析了C++中const修饰符的应用场景,包括const_iterator的使用限制、const指针与指向const对象的指针的区别,以及typedef与const结合使用时的注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1关于const_iterator

当我们对const_iterator解引用时,可以得到一个指向const对象的引用,同任何常量一样,该对象不可重写。

例子:vector<int>::const_iterator iter=text.begin();

*iter=9;//这里是错误的,该迭代器指向的对象为const对象,不可更改。

2 声明一个const类型的iterator

vector<int> num(10);

const vector<int>::iterator iter=num.begin();

 iter++; //error,can not chang the value of iter

*iter=10; //ok can change its underlying element

3.指向const对象的指针。

const char * cptr;

表明cptr指向的是一个const对象,不可随意更改。类似上面的const_iterator

4.const指针。

int errNumb=0;

int * const curErr=&errNumb;

表明curErr不可再指向其他地址了。


5指针与typedef

typedef string *pstring;

const pstring cstr;

一旦指针域typedef结合,千万别简单的认为文本扩展了。

正确分析:cstr是一个const pstring类型。在第一句中pstring为指向string对象的指针,经过const之后

意思是将cstr声明为指向string对象的const指针。

下面是关于其等价表达式:

----string *const cstr;

--typedef string *pstring;

  pstring const cstr;

.text:100F8C1E ?getFunctionIndirectType@UnDecorator@@CA?AVDName@@ABV2@@Z proc near .text:100F8C1E ; CODE XREF: UnDecorator::getPrimaryDataType(DName const &)+13C↓p .text:100F8C1E ; UnDecorator::getPtrRefType(DName const &,DName const &,char const *)+63↓p .text:100F8C1E .text:100F8C1E var_38 = DName ptr -38h .text:100F8C1E var_30 = DName ptr -30h .text:100F8C1E returnType = DName ptr -28h .text:100F8C1E var_20 = DName ptr -20h .text:100F8C1E rd = DName ptr -18h .text:100F8C1E thisType = DName ptr -10h .text:100F8C1E fitType = DName ptr -8 .text:100F8C1E result = dword ptr 8 .text:100F8C1E superType = dword ptr 0Ch .text:100F8C1E .text:100F8C1E push ebp .text:100F8C1F mov ebp, esp .text:100F8C21 mov ecx, ?gName@UnDecorator@@0PBDB ; char const * const UnDecorator::gName .text:100F8C27 sub esp, 38h .text:100F8C2A mov al, [ecx] .text:100F8C2C test al, al .text:100F8C2E jnz short loc_100F8C48 .text:100F8C30 push [ebp+superType] ; rd .text:100F8C33 push 1 ; st .text:100F8C35 push [ebp+result] ; result .text:100F8C38 call ??H@YA?AVDName@@W4DNameStatus@@ABV0@@Z ; operator+(DNameStatus,DName const &) .text:100F8C3D add esp, 0Ch .text:100F8C40 .text:100F8C40 loc_100F8C40: ; CODE XREF: UnDecorator::getFunctionIndirectType(DName const &)+40↓j .text:100F8C40 mov eax, [ebp+result] .text:100F8C43 jmp loc_100F9023 .text:100F8C48 ; --------------------------------------------------------------------------- .text:100F8C48 .text:100F8C48 loc_100F8C48: ; CODE XREF: UnDecorator::getFunctionIndirectType(DName const &)+10↑j .text:100F8C48 cmp al, 36h ; &#39;6&#39; .text:100F8C4A jl short loc_100F8C50 .text:100F8C4C cmp al, 39h ; &#39;9&#39; .text:100F8C4E jle short loc_100F8C60 .text:100F8C50 .text:100F8C50 loc_100F8C50: ; CODE XREF: UnDecorator::getFunctionIndirectType(DName const &)+2C↑j .text:100F8C50 cmp al, 5Fh ; &#39;_&#39; .text:100F8C52 jz short loc_100F8C60 .text:100F8C54 mov ecx, [ebp+result] ; this .text:100F8C57 push 2 ; st .text:100F8C59 call ??0DName@@QAE@W4DNameStatus@@@Z ; DName::DName(DNameStatus) .text:100F8C5E jmp short loc_100F8C40 .text:100F8C60 ; --------------------------------------------------------------------------- .text:100F8C60 .text:100F8C60 loc_100F8C60: ; CODE XREF: UnDecorator::getFunctionIndirectType(DName const &)+30↑j .text:100F8C60 ; UnDecorator::getFunctionIndirectType(DName const &)+34↑j .text:100F8C60 push ebx .text:100F8C61 movsx ebx, al .text:100F8C64 sub ebx, 36h ; &#39;6&#39; .text:100F8C67 inc ecx .text:100F8C68 mov ?gName@UnDecorator@@0PBDB, ecx ; char const * const UnDecorator::gName .text:100F8C6E cmp ebx, 29h ; &#39;)&#39; .text:100F8C71 jnz short loc_100F8CA2 .text:100F8C73 mov al, [ecx] .text:100F8C75 test al, al .text:100F8C77 jz short loc_100F8C90 .text:100F8C79 movsx ebx, al .text:100F8C7C sub ebx, 3Dh ; &#39;=&#39; .text:100F8C7F inc ecx .text:100F8C80 mov ?gName@UnDecorator@@0PBDB, ecx ; char const * const UnDecorator::gName .text:100F8C86 cmp ebx, 4 .text:100F8C89 jl short loc_100F8CAB .text:100F8C8B cmp ebx, 7 .text:100F8C8E jmp short loc_100F8CA9 .text:100F8C90 ; --------------------------------------------------------------------------- .text:100F8C90 .text:100F8C90 loc_100F8C90: ; CODE XREF: UnDecorator::getFunctionIndirectType(DName const &)+59↑j .text:100F8C90 push [ebp+superType] ; rd .text:100F8C93 push 1 ; st .text:100F8C95 push [ebp+result] ; result .text:100F8C98 call ??H@YA?AVDName@@W4DNameStatus@@ABV0@@Z ; operator+(DNameStatus,DName const &) .text:100F8C9D add esp, 0Ch .text:100F8CA0 jmp short loc_100F8CBD .text:100F8CA2 ; --------------------------------------------------------------------------- .text:100F8CA2 .text:100F8CA2 loc_100F8CA2: ; CODE XREF: UnDecorator::getFunctionIndirectType(DName const &)+53↑j .text:100F8CA2 test ebx, ebx .text:100F8CA4 js short loc_100F8CAB .text:100F8CA6 cmp ebx, 3 .text:100F8CA9 .text:100F8CA9 loc_100F8CA9: ; CODE XREF: UnDecorator::getFunctionIndirectType(DName const &)+70↑j .text:100F8CA9 jle short loc_100F8CAE .text:100F8CAB .text:100F8CAB loc_100F8CAB: ; CODE XREF: UnDecorator::getFunctionIndirectType(DName const &)+6B↑j .text:100F8CAB ; UnDecorator::getFunctionIndirectType(DName const &)+86↑j .text:100F8CAB or ebx, 0FFFFFFFFh .text:100F8CAE .text:100F8CAE loc_100F8CAE: ; CODE XREF: UnDecorator::getFunctionIndirectType(DName const &):loc_100F8CA9↑j .text:100F8CAE cmp ebx, 0FFFFFFFFh .text:100F8CB1 jnz short loc_100F8CC5 .text:100F8CB3 mov ecx, [ebp+result] ; this .text:100F8CB6 push 2 ; st .text:100F8CB8 call ??0DName@@QAE@W4DNameStatus@@@Z ; DName::DName(DNameStatus) .text:100F8CBD .text:100F8CBD loc_100F8CBD: ; CODE XREF: UnDecorator::getFunctionIndirectType(DName const &)+82↑j .text:100F8CBD mov eax, [ebp+result] .text:100F8CC0 jmp loc_100F9022 .text:100F8CC5 ; --------------------------------------------------------------------------- .text:100F8CC5 .text:100F8CC5 loc_100F8CC5: ; CODE XREF: UnDecorator::getFunctionIndirectType(DName const &)+93↑j .text:100F8CC5 and [ebp+thisType.node], 0 .text:100F8CC9 and dword ptr [ebp+thisType._bf_4], 0FFFF0000h .text:100F8CD0 push esi .text:100F8CD1 mov esi, [ebp+superType] .text:100F8CD4 push edi .text:100F8CD5 mov edi, ebx .text:100F8CD7 mov eax, [esi] .text:100F8CD9 mov [ebp+fitType.node], eax .text:100F8CDC mov eax, [esi+4] .text:100F8CDF mov dword ptr [ebp+fitType._bf_4], eax .text:100F8CE2 and edi, 2 .text:100F8CE5 jz loc_100F8DD7 .text:100F8CEB cmp byte ptr [ecx], 40h ; &#39;@&#39; .text:100F8CEE jz loc_100F8D96 .text:100F8CF4 lea eax, [ebp+fitType] .text:100F8CF7 push eax ; rd .text:100F8CF8 lea eax, [ebp+rd] .text:100F8CFB push eax ; result .text:100F8CFC push offset asc_10131938 ; "::" .text:100F8D01 lea ecx, [ebp+var_20] ; this .text:100F8D04 call ??0DName@@QAE@PBD@Z ; DName::DName(char const *) .text:100F8D09 mov ecx, eax ; this .text:100F8D0B call ??HDName@@QBE?AV0@ABV0@@Z ; DName::operator+(DName const &) .text:100F8D10 mov eax, [ebp+rd.node] .text:100F8D13 mov [ebp+fitType.node], eax .text:100F8D16 mov eax, dword ptr [ebp+rd._bf_4] .text:100F8D19 mov dword ptr [ebp+fitType._bf_4], eax .text:100F8D1C mov eax, ?gName@UnDecorator@@0PBDB ; char const * const UnDecorator::gName .text:100F8D21 cmp byte ptr [eax], 0 .text:100F8D24 jz short loc_100F8D69 .text:100F8D26 lea eax, [ebp+var_30] .text:100F8D29 push eax ; result .text:100F8D2A call ?getScope@UnDecorator@@CA?AVDName@@XZ ; UnDecorator::getScope(void) .text:100F8D2F pop ecx .text:100F8D30 push eax ; rd .text:100F8D31 lea eax, [ebp+var_20] .text:100F8D34 push eax ; result .text:100F8D35 push 20h ; &#39; &#39; ; ch .text:100F8D37 lea ecx, [ebp+var_38] ; this .text:100F8D3A call ??4DName@@QAEAAV0@D@Z ; DName::operator=(char) .text:100F8D3F mov ecx, eax ; this .text:100F8D41 call ??HDName@@QBE?AV0@ABV0@@Z ; DName::operator+(DName const &) .text:100F8D46 mov eax, [ebp+var_20.node] .text:100F8D49 lea ecx, [ebp+rd] ; this .text:100F8D4C mov [ebp+rd.node], eax .text:100F8D4F mov eax, dword ptr [ebp+var_20._bf_4] .text:100F8D52 mov dword ptr [ebp+rd._bf_4], eax .text:100F8D55 lea eax, [ebp+fitType] .text:100F8D58 push eax ; rd .text:100F8D59 call ??YDName@@QAEAAV0@ABV0@@Z ; DName::operator+=(DName const &) .text:100F8D5E mov eax, [ebp+rd.node] .text:100F8D61 mov [ebp+fitType.node], eax .text:100F8D64 mov eax, dword ptr [ebp+rd._bf_4] .text:100F8D67 jmp short loc_100F8D8B .text:100F8D69 ; --------------------------------------------------------------------------- .text:100F8D69 .text:100F8D69 loc_100F8D69: ; CODE XREF: UnDecorator::getFunctionIndirectType(DName const &)+106↑j .text:100F8D69 lea eax, [ebp+fitType] .text:100F8D6C push eax ; rd .text:100F8D6D lea eax, [ebp+var_20] .text:100F8D70 push eax ; result .text:100F8D71 push 1 ; st .text:100F8D73 lea ecx, [ebp+var_38] ; this .text:100F8D76 call ??0DName@@QAE@W4DNameStatus@@@Z ; DName::DName(DNameStatus) .text:100F8D7B mov ecx, eax ; this .text:100F8D7D call ??HDName@@QBE?AV0@ABV0@@Z ; DName::operator+(DName const &) .text:100F8D82 mov eax, [ebp+var_20.node] .text:100F8D85 mov [ebp+fitType.node], eax .text:100F8D88 mov eax, dword ptr [ebp+var_20._bf_4] .text:100F8D8B .text:100F8D8B loc_100F8D8B: ; CODE XREF: UnDecorator::getFunctionIndirectType(DName const &)+149↑j .text:100F8D8B mov ecx, ?gName@UnDecorator@@0PBDB ; char const * const UnDecorator::gName .text:100F8D91 mov dword ptr [ebp+fitType._bf_4], eax .text:100F8D94 jmp short loc_100F8D9D .text:100F8D96 ; --------------------------------------------------------------------------- .text:100F8D96 .text:100F8D96 loc_100F8D96: ; CODE XREF: UnDecorator::getFunctionIndirectType(DName const &)+D0↑j .text:100F8D96 inc ecx .text:100F8D97 mov ?gName@UnDecorator@@0PBDB, ecx ; char const * const UnDecorator::gName .text:100F8D9D .text:100F8D9D loc_100F8D9D: ; CODE XREF: UnDecorator::getFunctionIndirectType(DName const &)+176↑j .text:100F8D9D mov al, [ecx] .text:100F8D9F test al, al .text:100F8DA1 jz loc_100F8E4B .text:100F8DA7 cmp al, 40h ; &#39;@&#39; .text:100F8DA9 jnz loc_100F8E44 .text:100F8DAF mov eax, ?disableFlags@UnDecorator@@0KA ; ulong UnDecorator::disableFlags .text:100F8DB4 inc ecx .text:100F8DB5 and eax, 60h .text:100F8DB8 mov ?gName@UnDecorator@@0PBDB, ecx ; char const * const UnDecorator::gName .text:100F8DBE cmp al, 60h ; &#39;`&#39; .text:100F8DC0 lea eax, [ebp+var_38] .text:100F8DC3 push eax ; result .text:100F8DC4 jz short loc_100F8E33 .text:100F8DC6 call ?getThisType@UnDecorator@@CA?AVDName@@XZ ; UnDecorator::getThisType(void) .text:100F8DCB pop ecx .text:100F8DCC mov ecx, [eax] .text:100F8DCE mov eax, [eax+4] .text:100F8DD1 mov [ebp+thisType.node], ecx .text:100F8DD4 mov dword ptr [ebp+thisType._bf_4], eax .text:100F8DD7 .text:100F8DD7 loc_100F8DD7: ; CODE XREF: UnDecorator::getFunctionIndirectType(DName const &)+C7↑j .text:100F8DD7 ; UnDecorator::getFunctionIndirectType(DName const &)+224↓j .text:100F8DD7 test bl, 4 .text:100F8DDA jz loc_100F8E77 .text:100F8DE0 mov eax, ?disableFlags@UnDecorator@@0KA ; ulong UnDecorator::disableFlags .text:100F8DE5 shr eax, 1 .text:100F8DE7 not eax .text:100F8DE9 test al, 1 .text:100F8DEB lea eax, [ebp+var_38] .text:100F8DEE push eax ; result .text:100F8DEF jz short loc_100F8E68 .text:100F8DF1 call ?getBasedType@UnDecorator@@CA?AVDName@@XZ ; UnDecorator::getBasedType(void) .text:100F8DF6 pop ecx .text:100F8DF7 push eax ; rd .text:100F8DF8 lea eax, [ebp+var_20] .text:100F8DFB push eax ; result .text:100F8DFC push 20h ; &#39; &#39; ; ch .text:100F8DFE lea ecx, [ebp+var_30] ; this .text:100F8E01 call ??4DName@@QAEAAV0@D@Z ; DName::operator=(char) .text:100F8E06 mov ecx, eax ; this .text:100F8E08 call ??HDName@@QBE?AV0@ABV0@@Z ; DName::operator+(DName const &) .text:100F8E0D mov eax, [ebp+var_20.node] .text:100F8E10 lea ecx, [ebp+rd] ; this .text:100F8E13 mov [ebp+rd.node], eax .text:100F8E16 mov eax, dword ptr [ebp+var_20._bf_4] .text:100F8E19 mov dword ptr [ebp+rd._bf_4], eax .text:100F8E1C lea eax, [ebp+fitType] .text:100F8E1F push eax ; rd .text:100F8E20 call ??YDName@@QAEAAV0@ABV0@@Z ; DName::operator+=(DName const &) .text:100F8E25 mov eax, [ebp+rd.node] .text:100F8E28 mov [ebp+fitType.node], eax .text:100F8E2B mov eax, dword ptr [ebp+rd._bf_4] .text:100F8E2E mov dword ptr [ebp+fitType._bf_4], eax .text:100F8E31 jmp short loc_100F8E77 .text:100F8E33 ; --------------------------------------------------------------------------- .text:100F8E33 为什么只在text:100F8D1C mov eax, ?gName@UnDecorator@@0PBDB ; char const * const UnDecorator::gName 崩溃 在其他地方也用到了gname
07-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值