2013-05-10 89 views
-2

我想對我的代碼發表意見。我需要一個隨機訪問迭代器來定製容器。 如果我讀的C++標準2011,特別是第24章,我明白了一個實現 可能是以下幾點:在C++ 2011中創建自定義隨機訪問迭代器

class my_iterator : public std::iterator<std::random_access_iterator_tag, my_element> 
{ 
    private: 
     my_container *_ref; 
     my_iterator_state state; // some useful state 
    public: 
     my_iterator(); 
     my_iterator (const my_iterator &); 
     my_iterator (my_iterator &&); 
     ~my_iterator(); 
     my_iterator &operator = (const my_iterator &); 
     my_iterator &operator = (my_iterator &&); 
     reference operator *(); 
     my_iterator &operator ++(); 
     bool operator != (my_iterator); 
     value_type operator *(); 
     pointer operator ->(); 
     my_iterator &operator *(); 
     void operator ++ (int); 
     value_type operator ++ (int); 
     const my_iterator &operator ++ (int); 
     reference operator ++ (int); 
     my_iterator &operator --(); 
     const my_iterator operator -- (int); 
     reference operator -- (int); 
     my_iterator &operator += (difference_type); 
     my_iterator operator + (difference_type); 
     my_iterator operator - (difference_type); 
     reference operator [] (difference_type); 
     bool operator < (my_iterator); 
     bool operator > (my_iterator); 
     bool operator <= (my_iterator); 
     bool operator >= (my_iterator); 
     friend difference_type (my_iterator, my_iterator); 
     friend my_iterator operator + (difference_type, my_iterator); 
     friend void swap (iterator &, iterator &); 
}; 

void swap (iterator &, iterator &); 
difference_type (my_iterator, my_iterator); 
my_iterator operator + (difference_type, my_iterator); 

以上實施是正確的,功能齊全,?有太多成員?

+4

呵呵,是的。請自己做一個服務並使用'boost :: iterator_facade'。 – 2013-05-10 14:31:16

+0

歡迎來到Stack Overflow。請花時間閱讀 [faq]。你會得到一個[徽章](http://stackoverflow.com/badges)(c: – 2013-05-10 14:32:35

+0

你可能會更好的問這個[codereview.se] – 2013-05-10 14:33:08

回答

3

一般來說,您不能使用具有相同參數但返回類型不同的重載,因此您的幾個成員不能存在。你應該記住以下:

reference operator *(); 
my_iterator operator ++ (int); 
my_iterator operator -- (int); 

並取出假重載:

value_type operator *(); 
void operator ++ (int); 
value_type operator ++ (int); 
const my_iterator &operator ++ (int); 
reference operator ++ (int); 
const my_iterator operator -- (int); 
reference operator -- (int); 

這是沒有意義的:

friend difference_type (my_iterator, my_iterator); 

,我認爲應該是:

friend difference_type operator-(my_iterator, my_iterator); 

,你似乎失蹤了operator-=

+0

確實是'friend difference_type(my_iterator,my_iterator);'是打字錯誤:) – 2013-05-10 18:02:46

相關問題