Mastering C++: A Comprehensive Quiz Based on 'Thinking in C++'

Disable ads (and more) with a membership for a one time $2.99 payment

Test your C++ skills with our quiz based on Bruce Eckel's 'Thinking in C++'. Dive into object-oriented programming, advanced topics, and fundamentals. Perfect for learners and experts alike. Assess your knowledge and become a C++ master!

Practice this question and more.


How is an 'iterator' declared in relation to its container class?

  1. As a friend class

  2. As a private member

  3. As a public member

  4. As a derived class

The correct answer is: As a friend class

An iterator is a special object used to iterate or traverse through the elements of a container class, such as a vector or list. In order to declare an iterator, it is necessary to establish a direct relationship with its associated container class. The correct way to do this is by declaring the iterator as a friend class of the container class. This allows the iterator class to have access to the private and protected members of the container class, which is necessary for it to function properly. Declaring the iterator as a private or public member would not establish the necessary relationship, and declaring it as a derived class would create unnecessary dependencies between the iterator and the container class. Therefore, the correct and most efficient way to declare an iterator is as a friend class of the container class.