我可以使用c
的参考变量(例如,Customer &c
)吗?如果是这样如何?这是更喜欢使用指针变量?C++参考vs指针for循环内变量?
for(std::vector<Customer*>::const_iterator it = customers_.begin();
it != customers_.end() ; it ++)
{
Customer *c = (*it);
c->PrintName();
...
}
为什么不直接使用迭代器? IT-> PrintName(); – Ferruccio
@Ferruccio:因为迭代器引用了一个指针,而不是一个对象。你必须做'(* it) - >' –
我明白了。我错过了。 – Ferruccio