Hi,

I used to have difficulty with more advanced bits of scope but eventually understood but after a long break from c++ i have of course forgotten bits. my problem is with this bit of code:

std::vector<int>::iterator it

as i understand it this bit of code creates an iterator for a vector of ints.

What i do not understand is what is going on with/after the second ::


e.g. i understand

std::vector<int>

i.e. look in the std namespace for the class template for vectors and create an integer version of it.

But how can you go further? is iterator a member of vector? as in

car::engine

?

Thanks

>What i do not understand is what is going on with/after the second ::
Work it from the inside out. iterator is a member of the vector<int> class, and vector<int> is a member of the std namespace. The idea is implemented as such:

namespace std {
  template <typename T>
  class vector {
  public:
    typedef T *iterator;
  };
}

>What i do not understand is what is going on with/after the second ::
Work it from the inside out. iterator is a member of the vector<int> class, and vector<int> is a member of the std namespace. The idea is implemented as such:

namespace std {
  template <typename T>
  class vector {
  public:
    typedef T *iterator;
  };
}

Cool thanks Narue.

Quick question though:
1) How come you can instantiate an iterator object when the class containing it has not been instantiated? Or is it implicitly instantiated?


Thanks for your time

>1) How come you can instantiate an iterator object when
>the class containing it has not been instantiated?

It's not an iterator object, it's a typedef restricted to the class scope.

i'm not sure i understand what is it a typedef of?

Thanks

>i'm not sure i understand what is it a typedef of?
That's irrelevant to the original question. The typedef has a scope of the class, and thus is accessed as <class>::<typedef>. Then the class is a member of a namespace, so the fully qualified name is <namespace>::<class>::<typedef>.

The underlying type for the typedef doesn't matter unless you're interested in the implementation of std::vector, but that's a separate question.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.