久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

修改 C++ 中的 const int

Modifying a const int in C++(修改 C++ 中的 const int)
本文介紹了修改 C++ 中的 const int的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

限時(shí)送ChatGPT賬號..

運(yùn)行下面的代碼顯示&x=ptr,那么x和*ptr怎么不相等?

running the following code shows that &x=ptr, so how come x and *ptr are not equal?

const int x=10;
int* ptr =(int*) &x;
*ptr = (*ptr)+1;

cout << &x << " " << x << "  " << ptr <<"  " <<*ptr;  //output : 0012FF60 10  0012FF60  11

推薦答案

C++ 實(shí)現(xiàn)只需要您遵守規(guī)則才能使程序運(yùn)行.你違反了規(guī)則.C++ 實(shí)現(xiàn)可能是這樣的:

The C++ implementation is only required to make a program work if you obey the rules. You violated the rules. The C++ implementation likely behaved this way:

  • 因?yàn)?x 被聲明為 const,C++ 實(shí)現(xiàn)知道只要你遵守規(guī)則,它的值就不會(huì)改變.因此,無論在何處使用 x,C++ 實(shí)現(xiàn)都會(huì)使用 10,而無需費(fèi)心檢查 x 是否已更改.
  • 因?yàn)?*ptr 指向一個(gè)非常量 int,所以實(shí)際執(zhí)行了存儲(chǔ)和讀取操作.這些工作"是因?yàn)樗赶虻膬?nèi)存(表示 x 的地方)實(shí)際上并未被操作系統(tǒng)標(biāo)記為只讀.因此,您可以進(jìn)行修改,盡管您不應(yīng)該這樣做.
  • Because x is declared const, the C++ implementation knows its value cannot change as long as you obey the rules. So, wherever x is used, the C++ implementation uses 10 without bothering to check whether x has changed.
  • Because *ptr points to a non-const int, stores to it and reads from it are actually performed. These "work" because the memory it points to (where x is represented) is not actually marked read-only by the operating system. Thus, you are able to make modifications in spite of the fact that you are not supposed to.

請注意,如果您遵守規(guī)則,C++ 實(shí)現(xiàn)的行為將可以工作.如果您沒有修改 x,那么在 x 出現(xiàn)的任何地方使用 10 都會(huì)正常工作.或者,如果您沒有將 x 聲明為 const,那么 C++ 實(shí)現(xiàn)不會(huì)假定它總是 10,因此每當(dāng) x 被訪問.這就是 C++ 標(biāo)準(zhǔn)對實(shí)現(xiàn)的所有要求:如果你遵守規(guī)則,它就可以工作.

Observe that the behavior of the C++ implementation would work if you obeyed the rules. If you had not modified x, then using 10 for x wherever it appeared would have worked normally. Or, if you had not declared x to be const, then the C++ implementation would not have assumed it would always be 10, so it would get the changed value whenever x was accessed. This is all the C++ standard requires of an implementation: That it work if you follow the rules.

當(dāng)您不遵守規(guī)則時(shí),C++ 實(shí)現(xiàn)可能會(huì)以看似不一致的方式中斷.

When you do not follow the rules, a C++ implementation may break in seemingly inconsistent ways.

這篇關(guān)于修改 C++ 中的 const int的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Difference between std::reference_wrapper and simple pointer?(std::reference_wrapper 和簡單指針的區(qū)別?)
Difference between const. pointer and reference?(常量之間的區(qū)別.指針和引用?)
How to access the contents of a vector from a pointer to the vector in C++?(c++ - 如何從指向向量的指針訪問向量的內(nèi)容?)
Meaning of *amp; and **amp; in C++(*amp; 的含義和**amp;在 C++ 中)
Why can#39;t I do polymorphism with normal variables?(為什么我不能對普通變量進(jìn)行多態(tài)?)
Dereferencing deleted pointers always result in an Access Violation?(取消引用已刪除的指針總是會(huì)導(dǎo)致訪問沖突?)
主站蜘蛛池模板: 久久国产精品一区 | 国产精品久久久久久久粉嫩 | 国产精品久久久久久久久久三级 | 国产91av视频在线观看 | 色久五月 | 精品一区av| 亚洲第一成年免费网站 | 丝袜一区二区三区 | 欧美一级三级 | 亚洲性视频 | 久久久久国色av免费观看性色 | 亚洲国产一区在线 | 日韩在线一区二区 | 久草免费在线视频 | 凹凸日日摸日日碰夜夜 | 99久热在线精品视频观看 | 亚洲日日夜夜 | 精品福利在线 | 国产91久久精品一区二区 | 午夜爱爱毛片xxxx视频免费看 | 国产日韩视频 | 九九九久久国产免费 | 国产高清在线精品一区二区三区 | 亚洲一区二区三区四区视频 | 九九热在线视频 | 中文在线视频观看 | 99精品观看 | 国产99久久久久 | 91视频导航 | 久久久青草婷婷精品综合日韩 | 久操亚洲 | 91性高湖久久久久久久久_久久99 | 国产真实精品久久二三区 | 日韩精品一区二区三区中文在线 | 欧美不卡 | 成人免费观看男女羞羞视频 | 狠狠干2020 | 久久久久久久久久久久久91 | 欧美一级特黄aaa大片在线观看 | 在线视频成人 | 成人精品国产一区二区4080 |