我想访问这些对象列表中包含的对象的成员。访问对象列表中的非常量成员
我有一个类CApp
与成员std::list<Particle> PList
作为粒子列表。
一个类Particle
有一个成员void Update()
从我的理解const不能是const,因为它影响实例(euler集成和东西)。
我想遍历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
不知道如何处理这事,如果需要更多的信息/解释,让我知道。
在此先感谢!
您能否详细说明参考与指针?如何和一点为什么? – Miles 2011-03-22 06:29:04
@Miles:查看本FAQ:http://www.parashift.com/c++-faq-lite/references.html#faq-8.6 – Naveen 2011-03-22 07:19:57