为什么 C++对指针数据强制 const ?

发布于 2024-11-06 02:40:24 字数 578 浏览 2 评论 0原文

可能的重复:
为什么 const 限定符不起作用在 const 对象的指针成员上?

考虑以下具有指针成员 int *a 的类。编译器允许 const 方法 constMod,即使它修改了指针数据。为什么编译器不在 const 方法的上下文中将指针数据设置为 const?如果 a 只是一个 int,我们就不允许在 const 方法中修改它。

class ConstTest
{
    public:

    ConstTest(int *p): a(p) {}

    void constMod() const {
        ++(*a);
    }

    int *a;
};

我在 Linux 上使用 g++。

Possible Duplicate:
Why isn't the const qualifier working on pointer members on const objects?

Consider the following class that has a pointer member int *a. The const method constMod is allowed by the compiler even though it modifies the pointer data. Why doesn't the compiler make the pointer data const in the context of the const method? If a was just an int we wouldn't be allowed to modify it in a const method.

class ConstTest
{
    public:

    ConstTest(int *p): a(p) {}

    void constMod() const {
        ++(*a);
    }

    int *a;
};

I'm using g++ on linux.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

薔薇婲 2024-11-13 02:40:24

constMod() 内部,a 的声明被视为:

int *const a;

这意味着指针有一个常量值,而不是它指向的内容到。听起来您希望它被视为:

const int *a;

这是不同的。

Inside constMod(), the declaration of a is treated as:

int *const a;

which means the pointer has a constant value, not what it points to. It sounds like you are expecting it to be treated as:

const int *a;

which is different.

ま昔日黯然 2024-11-13 02:40:24

指针本身没有被修改,只有指向的数据被修改。即使从人类的角度来看这听起来很奇怪,但从编译器的角度来看,类的任何成员都没有改变。

The pointer itself is not modified, only the data pointed. Even if it sounds strange from a human point of view, from a compiler point of view, none of the member of the class are altered.

走走停停 2024-11-13 02:40:24

这只是一个所有权问题......编译器无法知道所指向的对象在逻辑上是否是该对象的一部分,因此留给程序员来监管此类问题。 const 成员可以执行有副作用的操作,只要它们不修改自己的表观值即可。这与让他们调用 std::cout::operator<<() 或其他非常量函数没有什么不同......

It's just an ownership issue... there's no way the compiler can know if the pointed-to object is logically part of the object or not, so it's left for the programmer to police such issues. const members are allowed to perform operations with side effects, as long as they don't modify their own apparent value. It's not really any different from letting them call say std::cout::operator<<() or some other non-const function....

楠木可依 2024-11-13 02:40:24

在上面的例子中,const 是指针本身。您不能执行++a;。但是,它不会阻止您修改所指向的数据

What is const in the above case is the pointer itself. You are not allowed to do ++a;. However, it doesn't prevent you from modifying the data being pointed to.

倒带 2024-11-13 02:40:24

我相信是因为在这里您试图更改 datamember 指向的值。
如果您尝试修改数据成员,则会出错。

这意味着,如果您指向其他内容而不是更改值,则会出现错误

I believe because here you are trying to change the value where datamember is pointing to.
If you try to modify the data member it would be error.

Meaning, you'll get error if you make a point to something else instead of changing the value

浅浅 2024-11-13 02:40:24

const 成员函数不允许您修改它的成员。

在您的示例中,您有一个指向 int 的指针。

在 const 方法中,您正在修改它所指向的值,而不是指针本身。

尝试给出 ++a,这实际上会修改指针值,并且在您的 const 方法中是不允许的。

const member functions will not allow you to modify it's members.

In your example, you have a pointer that points an int.

And in the const method, you are modifying the value that it is pointed to but not the pointer itself.

Try giving, ++a, which will actually modify the pointer value and will not be allowed in your const method.

魂归处 2024-11-13 02:40:24

考虑

const T immutable_object0;
const T immutable_object1;
const T* mutable_view = ...a condition... ? &immutable_object0 : &immutable_object1;
// then later you reseat
mutable_view = ...another condition... ? &immutable_object0 : &immutable_object1;

or

const int immutable_data[120]
const int* mutable_view = immutable_data;
for(const int* end = immutable_data + 120; mutable_view != end; ++mutable_view) {
  // can't modify *mutable_view
}

是因为指针并不总是拥有所有权。在某些情况下,指针是对象的视图(第一个示例)或原始数组的迭代器(第二个示例)。对于这些情况,仅仅因为指向的数据是不可变的(或被视为不可变的)来限制指针上可用的操作是没有意义的。

第一个示例有点做作,但它的有效版本是当您使用指针成员来实现对象关联并且您不希望引用成员带来麻烦时。有时,指向的类型恰好是const。

Consider

const T immutable_object0;
const T immutable_object1;
const T* mutable_view = ...a condition... ? &immutable_object0 : &immutable_object1;
// then later you reseat
mutable_view = ...another condition... ? &immutable_object0 : &immutable_object1;

or

const int immutable_data[120]
const int* mutable_view = immutable_data;
for(const int* end = immutable_data + 120; mutable_view != end; ++mutable_view) {
  // can't modify *mutable_view
}

It's because pointers don't always have ownership. In some circumstances pointers are views to an object (first example) or are iterators into a raw array (second example). For those cases it doesn't make sense to restrict the operations available on the pointers just because the data pointed to is immutable (or seen as immutable).

The first example is a bit contrived but a valid version of it is when you use a pointer member to implement object association and you don't want the hassle of a reference member. Sometimes the pointed-to types happen to be const.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文