开发者

A question on vectors, pointers and iterators

开发者 https://www.devze.com 2022-12-30 14:34 出处:网络
Guys, I have a midterm examination tomorrow, and I was looking over the sample paper, and I\'m not sure about this question. Any help would开发者_StackOverflow中文版 be appreciated.

Guys, I have a midterm examination tomorrow, and I was looking over the sample paper, and I'm not sure about this question. Any help would开发者_StackOverflow中文版 be appreciated.

Let v be a vector<Thingie*>, so that each element v[i] contains a pointer to a Thingie. If p is a vector<Thingie*>::iterator, answer the following questions:

  1. what type is p?
  2. what type is *p?
  3. what code provides the address of the actual Thingie?
  4. what code provides the actual Thingie?


what type is p?

p is of type vector<Thingie*>::iterator, whatever type that happens to be.

what type is *p?

*p is a Thingie*&; that is, it is a reference to the element in the vector at which the iterator points.

what code provides the address of the actual Thingie?

*p, since the elements of the vector are pointers to the Thingies, and *p gives a reference to the element in the vector.

what code provides the actual Thingie?

**p; that is, you dereference the pointer obtained by *p to obtain the referent of the pointer.


p is vector::iterator

*p is a pointer to a Thingie

v[i]

*v[i]

0

精彩评论

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

关注公众号