How is iterator implemented?Code:vector<int> v; vector<int>::iterator i = v.begin();
I mean,
Then how is box and test related? And how does test class looks like?Code:test::box c = 1;
How is iterator implemented?Code:vector<int> v; vector<int>::iterator i = v.begin();
I mean,
Then how is box and test related? And how does test class looks like?Code:test::box c = 1;
Roughly speaking, iterator would be a type definition within the (templated) definition of the vector class. It's slightly more complicated than that as, conceptually at least, vector can be a specialisation of some other class, or might have some base class. The type definition within the definition of vector might, itself, be a definition of a class or struct type, or it might be a simple typedef.
Your "test::box" example is actually the more general case. test is generally a namespace, and box will be a type definition within that namespace. Classes and structs are associated with a namespace that has the same name (i.e. the member functions and data members of a class X exist in a namespace named X, which is why the X::member syntax works). Things are complicated somewhat, as test may also be an alias (eg resulting from typedef or macro expansion) of the actual class/namespace that contains the definition of box.
Note: in your post, you ignored the fact that vector is a templated class within the std namespace. The relationship between the vector's iterator and the vector (template) class is the same as the relationship between the vector template and the std namespace. Namespaces can be nested: there can be namespaces within namespaces, struct/class definitions (and their associated namespaces) within namespaces, namespaces within struct/class definitions, and struct/class definitions within struct/class definitions.
Last edited by grumpy; 06-09-2012 at 08:29 PM.
I didn't get you.
But Do you mean that iterator is class inside vector class? (And because we are accessing it using :: operator so it must be a static class)
Something like this:
Code:Template <class T> class vector { static class iterator { }; }; void main() { vector<int> v; vector<int>::iterator i = v.begin(); }
Last edited by freiza; 06-09-2012 at 10:00 PM.
You need to read more closely. Many answers you get here will often concern underlying concepts, rather than just spoon-feeding you code to illustrate. Because describing the concepts is both more precise and less verbose.
Anyway, to rephrase the first para of my previous post for the purpose of spoon-feeding .....
vector::iterator is a type, not a member of vector. The :: means it is in a specified scope, not that it is a static class.
One way might be
An alternative would beCode://within namespace std template < class T, class Allocator = allocator<T> > class vector // this is approximately what the standard specifies { public: class iterator { <whatever> }; // this is a class definition };
A particular case, which occurs in some (but not all) implementations of the standard library, isCode://within namespace std template < class T, class Allocator = allocator<T> > class vector { public: typedef some_type_related_to_T iterator; };
This description is both longer and less precise than my previous post.Code://within namespace std template < class T, class Allocator = allocator<T> > class vector { public: typedef T *iterator; typedef const T * const_iterator; // etc };
Last edited by grumpy; 06-09-2012 at 10:22 PM.