我想了解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',這不是你正在做的事情。 –