【发布时间】:2013-01-30 14:42:37
【问题描述】:
我正在寻找代码中的错误并且遇到了问题:
class a
{
public:
void foo(int a) {}
}
std::set<a*> set;
std::set<a*>::iterator it = set.begin();
it->foo(55); //gives me error:
// error: request for member ‘foo’ in ‘* it.std::_Rb_tree_const_iterator<_Tp>::operator-><a*>()’, which is of pointer type ‘a* const’ (maybe you meant to use ‘->’ ?)
为什么它不允许我在上面使用非常量函数?在不使用强制转换的情况下,我可以做些什么来拥有一组非常量指针?
【问题讨论】:
-
迭代器返回一个常量引用,因为你不能修改它引用的值(因为它被用作
std::set的键)。但这不是你的问题。 -
@Sander De Dycker 是的,这不是问题所在。双重取消引用一切正常。
-
顺便说一句:存储在
std::set中的指针是const。但它们不是 指向const的指针。
标签: c++ stl iterator set constants