Reputation: 151
I've some little question about STL iterator implementation.
Upvotes: 3
Views: 518
Reputation: 1
Like the best answer
class and struct is the same except the access.so use struct or class don't have many difference.
we know the C++ is include C
using struct seem simply and making a distinction between struct and class.
this is the define of iterator
struct output_iterator_tag{}
struct input_iterator_tag{}
struct forward_iterator_tag : public input_iterator_tag {};
struct bidirectional_iterator_tag : public forward_iterator_tag{}
struct random_accessl_iterator_tag : public bidirectional_iterator_tag{}
Upvotes: 0
Reputation: 227380
It is an implementation choice. class
and struct
are almost the same in C++
, the difference being the default access specifiers and inheritance, which are private for class and public for struct. So if a type has little need for private data members it may make more sense to implement it as a struct. But you can have exactly the same type implemented as either:
struct Iterator : IteratorBase {
SomeType x;
};
is exactly the same as
class Iterator : public IteratorBase{
public:
SomeType x;
};
Upvotes: 5
Reputation: 258558
This is defined in the standard, 24.2 which describes the <iterator>
header, which are struct
. This choice was probably made because iterators provide access to container elements and making them class
would be useless, the only difference being that class
has private
access level by default, whereas struct
has public
access level.
So there were 2 choices if declaring iterators class
instead of struct
:
public
, which is useless since the same effect can be obtained by making it directly struct
.Upvotes: 4
Reputation: 55887
Who says you that iterator is defined by struct? In standart there is nothing about it. Struct and class have only one difference - access to the elements as default. So, in struct by default access is public and in class by default access is private.
Upvotes: 0