访问对象列表中的非常量成员
我想访问此类对象列表中包含的对象的成员。
我有一个类 CApp
,其成员为 std::list
用作粒子列表。
类 Particle
有一个成员 void Update()
根据我对 const 的理解,它不能是 const,因为它会影响实例(欧拉积分等)。
我想迭代 PList 来更新所有粒子。
粒子构造函数包括:
Particle::Particle(std::list<Particle>* PList_In) {
PList = PList_In;
PList->push_back(*this);
}
多次调用以下内容:
Particle(&PList);
因此列表似乎已建立。作为旁注,如果有人能够解释此时内存中实际存在的内容(指针、引用),那就太好了。
但基本上这是错误的:
// Update all particles
std::list<Particle>::const_iterator iter;
for (iter = PList.begin(); iter != PList.end(); iter++) {
iter->Update();
}
不确定
error: passing ‘const Particle’ as ‘this’ argument of ‘void Particle::Update()’ discards qualifiers
如何处理这个,如果需要更多信息/解释,请告诉我。
提前致谢!
I would like to access members of objects contained in a list of such objects.
I have a class CApp
with a member std::list<Particle> PList
serving as a list of particles.
A class Particle
has a member void Update()
which from my understanding of const can't be const because it affects the instance (euler integration and stuff).
I would like to iterate through PList to update all of the particles.
A Particle constructor includes:
Particle::Particle(std::list<Particle>* PList_In) {
PList = PList_In;
PList->push_back(*this);
}
The following is called several times:
Particle(&PList);
So the list appears to be set up. As a side note, if anyone could explain what's actually there in terms of memory (pointers, references) at this point that would be great.
But basically this errs:
// Update all particles
std::list<Particle>::const_iterator iter;
for (iter = PList.begin(); iter != PList.end(); iter++) {
iter->Update();
}
with
error: passing ‘const Particle’ as ‘this’ argument of ‘void Particle::Update()’ discards qualifiers
Not sure what to do with this, if more information/explanation is required, let me know.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通过使用 const_iterator,您表示您不想更改列表元素。
如果需要,请使用 std::list::iterator。
By using const_iterator you say that you don't want to change list elements.
If you want to, use std::list::iterator.
通过使用const_iterator,您将告诉编译器您不会在修改指向对象的方法上使用此迭代器。方法是否修改对象由其 const 限定符决定。例如,如果您在
class A
中有一个类似int get() const;
的方法声明,那么此方法保证它不会修改class A< 的对象/代码>。在您的情况下,似乎
Update
没有 const 限定符,因此编译器抱怨您无法使用const_iterator
调用非常量函数。您需要更改迭代器类型std::list::iterator
。作为旁注,请考虑通过引用传递
list
对象,而不是在函数中使用指针。By using
const_iterator
you are telling the compiler that you are not going to use this iterator on methods which modify the pointed to objects. Whether a method modifies the object or not is determined by its const qualifier. For example if you have a method declaration likeint get() const;
insideclass A
then this method is guaranteeing that it will not modify objects ofclass A
. In your case it seemsUpdate
is not having the const qualifier hence the compiler is complaining that you can not call a non-const function using aconst_iterator
. You need to change the iterator typestd::list<Particle>::iterator
.As a side note consider passing the
list
object by reference instead of using the pointer in your function.