我是C的新手,我正在尝试使用LinkedListIterator实用程序类编写LinkedList类,如下所示. (我只列出了与问题相关的代码部分).我已将LinkedListIterator构造函数创建为私有.
现在,当我在main()中有这两行时,
LinkedListIterator iter = list->begin(); <<== No compilation error LinkedListIterator iter2; <<==== compilation error.
我得到第二行的编译错误,这是默认构造函数是私有的.但是,我不明白为什么第一行没有编译错误?为什么?什么叫代码的第一行?私有构造函数或复制构造函数或赋值运算符?
码
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. }