当Ccarray_foreach中删除一个对象时,就会发生问题
Problems occurs when removed one object within Cocos CCARRAY_FOREACH
背景:
-
可可版本 :3.0alpha
-
语言 :C
问题:
在一个ccarray_foreach中,当从上一个周期中删除一个对象时,它返回错误&重复的对象。
测试代码:
__Array* test = __Array::create();
test->retain();
Sprite *item1 = Sprite::create();
Sprite *item2 = Sprite::create();
Sprite *item3 = Sprite::create();
test->addObject(item1);
test->addObject(item2);
test->addObject(item3);
Object *it = NULL;
int index = 0;
CCARRAY_FOREACH(test, it)
{
log("[Enum] Index: %d Get: %X", index++, it);
}
it = NULL;
index = 0;
CCARRAY_FOREACH(test, it)
{
log("[Rmoved] Index: %d Get: %X", index++, it);
test->removeObject(it);
}
输出:
[枚举]索引:0获取:90DFB88
[枚举]索引:1获取:90E0030
[枚举]索引:2获取:90E04D8
[rmaved]索引:0获取:90DFB88
[rmaved]索引:1 get: 90E04D8
[rmaved]索引:2获取: 90E04D8
问题:
我做错了吗?我与互联网上发布的其他开发人员一起检查了代码。看起来几乎一样。
我很好奇以前没人遇到这个问题。
如果没有,我们必须在使用 ccarray_foreach ?
可能更好的解决方案应该使用std :: vector或cocos :: vector。
Vector<Sprite*>* test = new Vector<Sprite*>();
Sprite *item1 = Sprite::create();
Sprite *item2 = Sprite::create();
Sprite *item3 = Sprite::create();
test->pushBack(item1);
test->pushBack(item2);
test->pushBack(item3);
int index = 0;
Vector<Sprite*>::iterator it = test->begin();
while (it != test->end())
{
log("[Enum] Index: %d Get: %X", index++, *it);
++it;
}
index = 0;
it = test->begin();
while(it != test->end())
{
log("[Rmoved] Index: %d Get: %X", index++, *it);
if(isRemovable)
{
it = test->erase(it);
}
else
{
++it;
}
}
相关文章:
- 为什么我不能将一个对象push_back到属于另一个类的对象向量中?
- 在他自己的方法中,有可能将一个对象取消引用到另一个对象吗
- 从多个源构造一个对象,包括一个对象向量
- 为什么C++在将一个对象复制到另一个对象时需要对这两个对象进行低级常量限定
- 检查哪个对象调用了另一个对象的对象方法
- C++ 如何在将新对象分配给另一个对象时创建新对象
- 如何处理从一个对象传递到另一个在C++中具有公共抽象类的对象的消息
- 我有一个对象,它将在整个程序的持续时间内实例化,但一个类成员不会,我应该动态分配它吗?
- 包装一个对象并假装它是一个 int
- 为什么我可以在不重载 "=" 运算符的情况下将一个对象分配给另一个对象?
- 如何在qt中将信号和插槽与另一个对象连接 --解决了
- 如何使用函数将一个对象的输入复制到另一个对象中
- 选择一个元素而不是一个对象的数组的原因
- 为什么我可以使用 memcpy 将一个对象变量复制到另一个对象变量
- 如何将包含另一个对象向量的对象保存到文件中,并使用C++中的二进制文件从文件中读回?
- 编译问题 C++ 同时,尝试通过调用另一个对象中的成员函数来创建 std:: 线程
- 当Ccarray_foreach中删除一个对象时,就会发生问题
- C++初始化包含另一个对象设计问题的对象
- 从另一个对象调用成员函数时出现问题
- 在C++中将一个对象传递到另一个类的构造函数时遇到问题