我想了解const_iterator的含义。我有以下示例代码:了解带指针的const_iterator?
void CustomerService::RefreshCustomers()
{
for(std::vector<Customer*>::const_iterator it = customers_.begin();
it != customers_.end() ; it ++)
{
(*it)->Refresh();
}
}
Refresh()
处于Customer
类中的方法,它没有被定义为常量。起初我以为我认为const_iterator应该禁止修改容器的元素。但是,此代码无需投诉即可编译。这是因为有一个额外的间接进行的水平? const_iterator究竟做了什么?
UPDATE
而在这样的情况下,是否使用常量性最佳做法?
“修改”意味着'* it = some_other_thing',这不是你正在做的事情。 –