开发者

Does "vector<x>::iterator" mean there is a "vector<x>" namespace?

I have seen the code

vec开发者_如何学JAVAtor<char> v(10);
vector<char>::iterator p;

here what is the need of vector<char>::.Does it mean iterator is a class inside vector namespace?


Does it mean iterator is a class inside vector namespace?

Not quite, is a type inside vector class template. The iterator not only depends on the type of container (here a vector), but on the type of element iterated over as well (here a char).


Possibly the easiest way is to understand that :: is the scope operator, not just for namespaces.

std::vector<char> is a class, and therefore it has its own class scope (3.3.6 in C++03, 3.3.7 in C++11). std::vector<char>::iterator is a fully-qualified name in that scope. In the case of iterator, it names a type -- not necessarily a class, and even if it is the class itself is not necessarily defined in std::vector<char>, since iterator could be a typedef.

As it happens, a class scope is not one of those things that C++ calls a "namespace". In everyday[*] English, you could describe it as a kind of namespace, it just isn't the proper terminology in C++.

However you call it, though, be aware that it's vector<char> which is the class, and has the scope that contains iterator, not vector. std::vector does guarantee that any vector<T> has an iterator type, but for other templates it is not necessarily the case that every specialization has the same members and nested types. So there is no vector scope.

[*] "everyday", if your days are the kind of days had by people who talk a lot about namespaces.


Yes, it does mean exactly that. Iterator is defined within the scope of the class vector, and for each different type a vector is created, there's a different implementation of the iterator as well.


It also means that the iterator operates on a vector<char> instead of, say, a vector<int>.


Namespaces cannot be templetized, so vector cannot be a namespace. In fact vector is a template class (and vector is an instantiation) for which iterator is a nested type.

But the question has some point: the A::B syntax is normally not distinguishable. In term of name resolution, if fact, both classes an name-spaces are ... container of names.

Classes are more than name containers: they represent data having instances and associated functionalities. Namespaces are just container for names

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜