【发布时间】:2010-07-28 15:16:37
【问题描述】:
当我使用迭代器时,我有一个问题,似乎是某种隐式转换为 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的实现?