在模板之间共享模板结构

时间:2016-11-22 12:38:21

标签: c++ templates struct linked-list

在我实现链接列表的过程中,我创建了一个表示容器的类模板(CursorList),我有一个单独的类模板,应该是我的迭代器(CursorIterator)。

在我的CursorList中,我使用struct Node来表示列表的元素。我想与我的CursorIterator-Class共享这个结构(迭代器指向一个Node)。但是这并不像我想要的那样有效,我不能让两个类都知道Node的结构。

CursorList.h

#ifndef CURSORLIST_H
#define CURSORLIST_H

#include "CursorIterator.h"

template <class T> class CursorList {
public:
    CursorList() {}

    typedef T value_type;
    typedef CursorIterator<T> iterator;

    bool empty() const;
    int size() const;
    T& front() const;
    void push_front(const T& item);
    void pop_front();

    iterator begin() const;
    iterator end() const;
    iterator insert(iterator itr, const T& value);
    iterator erase(iterator start, iterator stop);
    iterator erase(iterator itr);

    struct Node {
        Node(const T& n_data, Node* n_prev, Node* n_next): data(n_data), prev(n_prev), next(n_next) {}
        T data;
        Node* prev;
        Node* next;
    };

private:
    Node* m_head;
};

#endif //CURSORLIST_H

CursorIterator.h

#ifndef CURSORITERATOR_H
#define CURSORITERATOR_H

template <class T> class CursorIterator {
private:
    typedef CursorIterator<T> iterator;
    Node* m_rep;

public:
    CursorIterator() {}
    CursorIterator(Node* n): m_rep(n) {}

    T& operator *();
    iterator& operator = (const iterator& rhs);
    bool operator != (const iterator& rhs) const;
    bool operator == (const iterator& rhs) const;
    iterator& operator ++();
    iterator operator ++(int);
};


#endif //CURSORITERATOR_H

0 个答案:

没有答案
相关问题