我在使用迭代器时遇到了一些隐式转换为const的问题。我不确定哪些代码是相关的(如果我知道,我可能就不会问这个问题了!),因此我将尽力说明我的问题。
typedef set<SmallObject> Container; //not const
void LargeObject::someFunction() { //not const
Container::iterator it; //not const
for (it = c.begin(); it != c.end(); ++it) { //assume c is a "Container"
(*it).smallObjectFunction(); //not a const function
}
}
然而,我总是遇到以下错误:
error: passing 'const SmallObject' as 'this' argument of 'int SmallObject::smallObjectFunction()' discards qualifiers
然而,如果我将其转换为
((SmallObject)(*it).smallObjectFunction();
,那么我就可以摆脱错误信息。我唯一能想到的是,某种方式上定义了
bool operator< (const SmallObject &a) const;
某种情况下,迭代器会返回const对象。这里需要帮助或解释吗?
SmallObject
的实现代码? - ULysses