访问对象列表中的非常量成员

发布于 2024-10-25 13:38:27 字数 927 浏览 1 评论 0原文

我想访问此类对象列表中包含的对象的成员。

我有一个类 CApp ,其成员为 std::list; PList 用作粒子列表。

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 技术交流群。

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

发布评论

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

评论(2

等风来 2024-11-01 13:38:27

通过使用 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.

别靠近我心 2024-11-01 13:38:27

通过使用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 like int get() const; inside class A then this method is guaranteeing that it will not modify objects of class A. In your case it seems Update is not having the const qualifier hence the compiler is complaining that you can not call a non-const function using a const_iterator. You need to change the iterator type std::list<Particle>::iterator.

As a side note consider passing the list object by reference instead of using the pointer in your function.

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