通过const指针修改用new创建的对象是否合法? [英] Is it legal to modify an object created with new through a const pointer?

查看:214
本文介绍了通过const指针修改用new创建的对象是否合法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,这个答案让我想起了这样的情况,你分配的结果 new 指向 const 的指针。 AFAIK,没有理由您不能合法地 const_cast 常量,实际修改对象在这种情况下:

So this answer made me think about the scenario where you assign the result of new to a pointer to a const. AFAIK, there's no reason you can't legally const_cast the constness away and actually modify the object in this situation:

struct X{int x;};

//....
const X* x = new X;
const_cast<X*>(x)->x = 0; // okay

但我想 - 如果你真的想要 new 创建一个 const 对象。所以我尝试了

But then I thought - what if you actually want new to create a const object. So I tried

struct X{};

//....
const X* x = new const X;

并编译!!!

这是GCC扩展还是标准行为?我已经从不在实践中看到这一点。

Is this a GCC extension or is it standard behavior? I have never seen this in practice. If it's standard, I'll start using it whenever possible.

推荐答案

const 是类型的一部分。无论您是否使用动态,静态或自动存储持续时间分配对象都无关紧要。它仍然是 const 。抛弃 const ness并改变对象仍然是一个未定义的操作。

const is part of the type. It doesn't matter whether you allocate your object with dynamic, static or automatic storage duration. It's still const. Casting away that constness and mutating the object would still be an undefined operation.

const ness是一个抽象,类型系统给我们实现不可变对象周围的安全;它在很大程度上帮助我们与只读存储器交互,但这并不意味着其语义限于这样的存储器。事实上,C ++ 甚至不知道是什么和不是只读内存。

constness is an abstraction that the type system gives us to implement safety around non-mutable objects; it does so in large part to aid us in interaction with read-only memory, but that does not mean that its semantics are restricted to such memory. Indeed, C++ doesn't even know what is and isn't read-only memory.

除此之外,通常的规则,没有例外[lol]为动态分配的对象,标准明确提到这一点(尽管在一个注释):

As well as this being derivable from all the usual rules, with no exception [lol] made for dynamically-allocated objects, the standards mention this explicitly (albeit in a note):


[C ++ 03:5.3.4 / 1]: new-expression 尝试创建 type-id (8.1)或 new-type-id 。该对象的类型是分配的类型。这个类型应该是一个完整的对象类型,而不是一个抽象类类型或其数组(1.8,3.9,10.4)。 [注意:由于引用不是对象,因此无法通过 new-expressions 创建引用。 ]

[C++03: 5.3.4/1]: The new-expression attempts to create an object of the type-id (8.1) or new-type-id to which it is applied. The type of that object is the allocated type. This type shall be a complete object type, but not an abstract class type or array thereof (1.8, 3.9, 10.4). [Note: because references are not objects, references cannot be created by new-expressions. ] [Note: the type-id may be a cv-qualified type, in which case the object created by the new-expression has a cv-qualified type. ] [..]

[C ++ 11:5.3.4 / 1]: new-expression 试图创建 type-id (8.1)或 new-type-id 。该对象的类型是分配的类型。这个类型应该是一个完整的对象类型,而不是一个抽象类类型或其数组(1.8,3.9,10.4)。它是实现定义的是否支持过度对齐类型(3.11)。 [注意:由于引用不是对象,因此无法通过 new-expressions 创建引用。 -end note] [注意: type-id 可能是一个cv限定的类型,在这种情况下, $ b

[C++11: 5.3.4/1]: The new-expression attempts to create an object of the type-id (8.1) or new-type-id to which it is applied. The type of that object is the allocated type. This type shall be a complete object type, but not an abstract class type or array thereof (1.8, 3.9, 10.4). It is implementation-defined whether over-aligned types are supported (3.11). [ Note: because references are not objects, references cannot be created by new-expressions. —end note ] [ Note: the type-id may be a cv-qualified type, in which case the object created by the new-expression has a cv-qualified type. —end note ] [..]

还有一个在 [C ++ 11:7.1.6.1/4] 中给出的用法示例。

There's also a usage example given in [C++11: 7.1.6.1/4].

不确定您期望的是什么。我不能说我自己做过这个,但我没有看到任何特别的原因不。可能有一些科技社会学家可以告诉你关于我们如何动态分配一些东西只是把它看作是不可变的。统计数据。

Not sure what else you expected. I can't say I've ever done this myself, but I don't see any particular reason not to. There's probably some tech sociologist who can tell you statistics on how rarely we dynamically allocate something only to treat it as non-mutable.

这篇关于通过const指针修改用new创建的对象是否合法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆