为什么 C++对指针数据强制 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
在
constMod()
内部,a
的声明被视为:这意味着指针有一个常量值,而不是它指向的内容到。听起来您希望它被视为:
这是不同的。
Inside
constMod()
, the declaration ofa
is treated as:which means the pointer has a constant value, not what it points to. It sounds like you are expecting it to be treated as:
which is different.
指针本身没有被修改,只有指向的数据被修改。即使从人类的角度来看这听起来很奇怪,但从编译器的角度来看,类的任何成员都没有改变。
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.
这只是一个所有权问题......编译器无法知道所指向的对象在逻辑上是否是该对象的一部分,因此留给程序员来监管此类问题。
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 saystd::cout::operator<<()
or some other non-const function....在上面的例子中,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.我相信是因为在这里您试图更改 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
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 yourconst
method.考虑
or
是因为指针并不总是拥有所有权。在某些情况下,指针是对象的视图(第一个示例)或原始数组的迭代器(第二个示例)。对于这些情况,仅仅因为指向的数据是不可变的(或被视为不可变的)来限制指针上可用的操作是没有意义的。
第一个示例有点做作,但它的有效版本是当您使用指针成员来实现对象关联并且您不希望引用成员带来麻烦时。有时,指向的类型恰好是const。
Consider
or
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
.