Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

STL - Why use the scope resolution operator for iterator

Tags:

c++

stl

To access an STL iterator, why do I need a scope resolution operator, and not a dot operator? Is it because the iterator is static, and does not belong to a particular class instance?

vector<int>::iterator my_iterator;

and not

vector<int> numbers;
numbers.iterator;
like image 679
Iceman Avatar asked Nov 26 '13 15:11

Iceman


2 Answers

Dot and arrow (->) operators are used to access all data (member variables, functions) that is specific to the given instance.

Scope resolution operator is used to access all data (static member variables, static functions, types) that is specific to the given type, not instance. Note that member types are never instance-specific so you will always use type::member_type to access them.

like image 183
p12 Avatar answered Sep 19 '22 06:09

p12


a::b names a type; a.b references a variable. In your example, my_iterator is the variable's name, and vector<int>::iterator is its type.

like image 36
meagar Avatar answered Sep 21 '22 06:09

meagar