I am new to C++ and i am trying to write a LinkedList class with a LinkedListIterator utility class as below. (i have listed only the code parts that are relevant to the question). I have created the LinkedListIterator constructor as private.
Now, when I have these two lines in main(),
LinkedListIterator iter = list->begin(); <<== No compilation error LinkedListIterator iter2; <<==== compilation error. i get the compilation error for the 2nd line, which is expected as the default constructor is private. However, I dont understand why there is no compilation error for the first line ? Why ? what is getting called for the first line of the code ? Private constructor or Copy constructor or assignment operator ?
Code
class LinkedListIterator { public: bool operator== (LinkedListIterator i) const; bool operator!= (LinkedListIterator i) const; void operator++ (); // Go to the next element int& operator* (); // Access the current element inline Node* hasnext(); inline Node* next(); private: LinkedListIterator(Node* p); <<==== Private constructor LinkedListIterator(); <<==== Private constructor Node* p_; friend class LinkedList;//LinkedList can construct a LinkedListIterator }; .... inline LinkedListIterator::LinkedListIterator(Node* p) : p_(p) { } inline LinkedListIterator::LinkedListIterator() { } inline LinkedListIterator LinkedList::begin() { return first_; } inline LinkedListIterator LinkedList::end() { return NULL; } ....... class LinkedList { public: void append(int elem); // Adds elem after the end void printList(); LinkedList() { first_ = NULL; } LinkedListIterator begin(); LinkedListIterator end(); LinkedListIterator erase(int elem); private: Node* first_; }; main() { LinkedList *list = new LinkedList(); list->append(1); list->append(2); list->append(3); LinkedListIterator iter = list->begin(); <<== No compilation error LinkedListIterator iter2; <<==== compilation error. }