Iceman
Iceman

Reputation: 4362

STL - Why use the scope resolution operator for iterator

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;

Upvotes: 7

Views: 1539

Answers (2)

p12
p12

Reputation: 1191

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.

Upvotes: 12

user229044
user229044

Reputation: 239311

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.

Upvotes: 1

Related Questions