const函数重载

 前段时间自己写了一个简单的List模板类,List类中有两个嵌套类,分别是const_iterator和iterator,其中后者是前者的子类。在重载List类的=操作符的时候,出现了一个编译上的问题,现在我把部分代码贴上来。
#ifndef LIST_H #define LIST_H #include <cstdlib> #include <cstdio> template <typename Object> class List { private: struct Node { Object data; struct Node *prev; struct Node *next; Node(const Object& d = Object(), Node *p = NULL, Node *n = NULL):data(d), prev(p), next(n) { } }; public: class const_iterator { protected: struct Node *current; //! @brief: constructor const_iterator(Node *p):current(p) {} Object& retrieve() const{ return<mce:script type="text/javascript" src="http://hi.images.csdn.net/js/blog/tiny_mce/themes/advanced/langs/zh.js" mce_src="http://hi.images.csdn.net/js/blog/tiny_mce/themes/advanced/langs/zh.js"></mce:script><mce:script type="text/javascript" src="http://hi.images.csdn.net/js/blog/tiny_mce/plugins/syntaxhl/langs/zh.js" mce_src="http://hi.images.csdn.net/js/blog/tiny_mce/plugins/syntaxhl/langs/zh.js"></mce:script> current->data; } friend class List<Object>; public: //! @brief: constructor const_iterator():current(NULL) {} //! @brief: get value const Object& operator*() const { return retrieve(); } //! @brief: prefix const const_iterator& operator++() { current = current->next; return *this; } //! @brief: suffix const const_iterator operator++(int) { const_iterator old = *this; ++(*this); return old; } //! @brief: prefix const const_iterator& operator--() { current = current->prev; return *this; } //! @brief: suffix const const_iterator operator--(int) { const_iterator old = *this; --(*this); return old; } //! @brief: equal b<mce:script type="text/javascript" src="http://hi.images.csdn.net/js/blog/tiny_mce/themes/advanced/langs/zh.js" mce_src="http://hi.images.csdn.net/js/blog/tiny_mce/themes/advanced/langs/zh.js"></mce:script><mce:script type="text/javascript" src="http://hi.images.csdn.net/js/blog/tiny_mce/plugins/syntaxhl/langs/zh.js" mce_src="http://hi.images.csdn.net/js/blog/tiny_mce/plugins/syntaxhl/langs/zh.js"></mce:script>ool operator==(const const_iterator& rhs) const{ return current == rhs.current; } //! @brief: not equal bool operator!=(const const_iterator& rhs) const{ return !(*this == rhs); } }; class iterator : public const_iterator { public: iterator() {} //! @brief: get value Object& operator*() { return this->retrieve(); } //! @brief: get value const Object& operator*() const { return const_iterator::operator*(); } //! @brief: prefix iterator& operator++() { this->current = this->current->next; return *this; } //! @brief: suffix iterator operator++(int) { iterator old = *this; ++(*this); return old; } //! @brief: prefix iterator& operator--() { this->current = this->current->prev; return *this; } //! @brief: suffix iterator operator--(int) { iterator old = *this; --(*this); return old; } protected: //! @brief: call father's constructor iterator(struct Node *p):const_iterator(p) { } friend class List<Object>; }; public: //! @brief: constructor List():head(NULL), tail(NULL), current_Size(0) { init(); } //! @brief: c<mce:script type="text/javascript" src="http://hi.images.csdn.net/js/blog/tiny_mce/themes/advanced/langs/zh.js" mce_src="http://hi.images.csdn.net/js/blog/tiny_mce/themes/advanced/langs/zh.js"></mce:script><mce:script type="text/javascript" src="http://hi.images.csdn.net/js/blog/tiny_mce/plugins/syntaxhl/langs/zh.js" mce_src="http://hi.images.csdn.net/js/blog/tiny_mce/plugins/syntaxhl/langs/zh.js"></mce:script>onstructor List(const List<Object>& rhs):head(NULL), tail(NULL), current_Size(0) { init(); *this = rhs; } //! @brief: destructor ~List() { clear(); delete head; delete tail; } //! @brief: clear all elements void clear() { while (!empty()) pop_front(); } //! @brief: judg empty bool empty() { return size() == 0; } //! @brief: size int size() { return current_Size; } //! @brief: pop front void pop_front() { erase(begin()); } //! @brief: pop back void pop_back() { erase(--end()); } //! @brief: Adds a new element at the end of the list, right after its current last element. void push_back(const Object& x) { insert(iterator(end()), x); } //! @brief: Inserts a new element at the beginning of the list, right before its current first element. void push_front(const Object& x) { insert(iterator(begin()), x); } //! @brief: Assigns as the new content for the container a copy of the elements in rhs. const List& operator=(const List& rhs) { if (this == &rhs) return *this; clear(); for (const_iterator itr=rhs.begin(); itr!=rhs.end(); ++itr) push_back(*itr); return *this; } //! @brief: first element const_iterator begin() const { return const_iterator(head->next); } //! @brief: first element iterator begin() { return iterator(head->next); } //! @brief: last element const_iterator end() const { return const_iterator(tail); } //! @brief: last element iterator end() { return iterator(tail); } //! @brief: Removes from the list container either a single element (position) or a range of elements ([first,last)). iterator erase(iterator itr) { iterator ret = iterator(itr.current->next); itr.current->next->prev = itr.current->prev; itr.current->prev->next = itr.current->next; delete itr.current; current_Size--; return ret; } iterator erase(iterator first, iterator last) { iterator itr; for (itr=first; itr!=last;) itr = erase(itr); return itr; } //! @brief: The list container is extended by inserting new elements before the element at position iterator insert(iterator position, const Object& x) { struct Node *p = new struct Node(x, position.current->prev, position.current); iterator ret = iterator(p); position.current->prev->next = p; position.current->prev = p; current_Size++; return ret; } //! @brief: This effectively increases the container size by the amount of elements inserted void insert(iterator position, int num, const Object& x) { for (int i=0; i<num; i++) insert(position, x); } //! @brief: Iterators specifying a range of elements. Copies of the elements in the range [first,last) are inserted at position position. void insert(iterator position, iterator first, iterator last) { for (iterator it=first; it!=last; ++it) insert(position, *it); } //! @brief: Exchanges the content of the vector by the content of lst, // which is another list object containing elements of the same type. // Sizes may differ. void swap(List<Object>& lst); //! @brief: Sorts the elements in the container from lower to higher. // The sorting is performed by comparing the elements // in the container in pairs using a sorting algorithm. void sort(); //! @brief: Reverse the order of elements void reverse(); //! @brief: Merge sorted lists. Merges x into the list, // inserting all the elements of x into the list object at their respective ordered positions. // This empties x and increases the list size. void merge(List<Object>& lst); //! @brief: print the list void printList() { for (iterator it=begin(); it!=end(); it++) printf("%d ", *it); printf("/n"); } //! @brief:Returns an iterator to the first element in the range [first,last) that compares equal to value, or last if not found. iterator find(iterator first, iterator last, const Object& x) { iterator itr; for (itr=first; itr!=last; ++itr) { if(*itr == x) break; } return itr; } //! @brief: Moves elements from list x into the list container at the specified position, // effectively inserting the specified elements into the container and removing them from x. void splice(iterator position, List<Object>& lst) { if (this == &lst) return; insert(position, lst.begin(), lst.end()); lst.clear(); } private: struct Node *head; struct Node *tail; int current_Size; //! @brief: initial void init() { head = new struct Node(); tail = new struct Node(); head->next = tail; tail->prev = head; current_Size = 0; } }; #endif
在154行的时候,编译器提示等号两边的类型不匹配,原因是我原来的代码是:iterator itr=rhs.begin()。在重载begin()函数的时候,编译器会自动为函数加入this指针参数,this指针也会成为匹配的条件之一。这个时候const函数加入的是const指针,非const函数加入的是普通指针。 所以160行和164行的两个begin()函数,也算是函数的重载。
rhs是一个const的引用,调用的是const函数begin,返回的是const_iterator类型的,由于const_iterator是父类,自然不能赋值给子类类型。

PS. 复习一下函数重载(overload function) 的概念:出现在相同作用域的两个函数,如果函数名称相同但是形参表不同,成为函数的重载。
函数的重载和重复声明的区别:如果两个函数的返回类型好行参表完全匹配,则将第二个函数声明视为第一个函数 的重复声明。如果两个函数行参表完全相同,但是返回类型不同,则第二个函数的声明是错误的。

你可能感兴趣的:(const函数重载)