简体   繁体   English

将对象复制到另一个对象时,为什么 C++ 需要对两个对象进行低级 const 限定?

[英]Why does C++ require low-level const qualification on both objects when copying an object to another?

I was reading C++-Primer (by Josée Lajoie and Stanley B. Lippman) when I came across this section about top-level and low-level const s.当我看到有关顶级和低级const的这一部分时,我正在阅读C++-Primer (由 Josée Lajoie 和 Stanley B. Lippman 撰写)。 On one paragraph, it says that when copying an object, top level const s are ignored.在一段中,它说复制对象时,顶级const被忽略。

Eg.例如。

const int i = 24;
int i2 = i;

Where the top-level const qualification in i is ignored when copied into i2 .当复制到i2时, i 中的顶级const限定被忽略。

However, it also states然而,它也指出

On the other hand, low-level const is never ignored.另一方面,低级const永远不会被忽略。 When we copy an object, both objects must have the same low-level const qualification or there must be a conversion between the types of the two objects.当我们复制一个对象时,两个对象必须具有相同的低级const限定,或者两个对象的类型之间必须进行转换。 In general, we can convert a non const to const but not the other way around.在一般情况下,我们可以转换为非const ,以const而不是周围的其他方法。

Now, usually when C++ has a rule like this there's a reason behind them.现在,通常当 C++ 有这样的规则时,它们背后是有原因的。 I find that understanding them is fundamental to understanding the rule and therefore making it easier to memorize (this is like understanding a mathematic concept instead of memorizing formulas).我发现理解它们是理解规则的基础,因此更容易记住(这就像理解一个数学概念而不是记住公式)。 However, there's no logical rule I find that fits into the puzzle here.然而,我发现没有符合这里谜题的逻辑规则。

As a summary, my question is:作为总结,我的问题是:

  1. Why is that when copying an object the top-level const qualification is ignored?为什么在复制对象时会忽略顶级const限定? (There's a section explaining why in the book too, but I can't seem to get it) (书中也有解释原因的部分,但我似乎无法理解)

Copying and object doesn't change the copied object.复制和对象不会改变复制的对象。 As a result, it is immaterial whether the object copied from or copied into is const因此,复制自或复制到的对象是否为const并不重要

  1. What's the reason behind how low-level const is never ignored when copying an object?复制对象时从不忽略低级const背后的原因是什么? (low-level const qualification is needed on both objects) (两个对象都需要低级const限定)

Top level case: You make a copy of the const object, so it doesn't matter if your copy is const or not, because (assuming const-correct types) you can't modify the original via the copy.顶级情况:您制作了 const 对象的副本,因此您的副本是否为 const 并不重要,因为(假设 const 类型正确)您无法通过副本修改原始对象。

Low level case: You make a copy of a handle to another object.低级情况:您将句柄复制到另一个对象。 The original handle does not allow modification of the object it refers to.原始句柄不允许修改它所引用的对象。 Allowing to ignore the low level const would mean you can obtain a handle that allows you to modify the referred object, breaking const correctness.允许忽略低级 const 意味着您可以获得一个句柄,允许您修改引用的对象,从而破坏 const 的正确性。

It would allow this kind of craziness:它会允许这种疯狂:

// should not modify n, right?
void foo(const int& n)
{
    int& mutable_n = n;
    mutable_n = 42;
}

...

int n = 0;
foo(n); // should not modify n, right?
std::cout << n << '\n'; // prints 42, wat??

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么C与C ++相反禁止在指向指针的两个级别添加const-qualification? - Why does C in contrast to C++ ban adding const-qualification to both levels of a pointer-to-pointer? 混合低级C / C ++代码 - Co-mingling low-level C/C++ code C ++低级文件加密功能 - C++ low-level file encryption function 为什么不能在模板函数中向局部变量添加低级const类型 - why can not add low-level const type to local variable in template function 为什么 C++ 需要用户提供的默认构造函数来默认构造一个 const 对象? - Why does C++ require a user-provided default constructor to default-construct a const object? Python 在反序列化 json 方面比 C++ 快。 为什么? Python 3 中的 json 库是用 C/C++ 还是其他低级语言编写的? - Python faster than C++ in deserializing json. Why? Is the json library in Python 3 written in C/C++ or other low-level language? 除非在XP兼容模式下运行,否则Windows Vista / 7上的低级C ++应用程序崩溃 - Low-Level C++ App Crashes on Windows Vista/7 Unless Run in XP Compatibility Mode C++ 中“低级”容器数据的原始指针、智能指针或 std::vector - Raw pointer, smart pointer or std::vector for “low-level” container data in C++ C++ 中“低级”多线程的基本示例是什么? - What is a basic example of "low-level" multi-threading in C++? 如何在Windows上使用C ++访问低级硬件I / O功能? - How can I access a low-level hardware I/O functions in C++ on Windows?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM