开发者

the meaninings of two ** for an iterator

In the f开发者_运维问答ollowing code segment

vector<SceneObject *> sceneObjs;
vector<SceneObject *>::iterator iter;

iter = sceneObjs.begin();
while (iter != sceneObjs.end()){
  cout << **iter <<endl;
  iter++;
}

why **iter has two *s ?


The first * dereferences the iterator, giving a SceneObject * pointer. The second * dereferences this SceneObject * pointer to the SceneObject itself.


Because *iter is a SceneObject *& - a SceneObject pointer. You need to dereference it to get to the real SceneObject.


Because *iter returns a SceneObject* which will then be again dereferenced by the second *.


The first * returns the vale in the iterator, a SceneObject* pointer. The second * deferences that pointer, giving a SceneObject. I suspect there's an overload for << that renders the SceneObject` to a stream.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜