开发者

the meaninings of two ** for an iterator

开发者 https://www.devze.com 2023-04-12 18:02 出处:网络
In the f开发者_运维问答ollowing code segment vector<SceneObject *> sceneObjs; vector<SceneObject *>::iterator iter;

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

精彩评论

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