vendredi 22 juillet 2016

const_iterator vs iterator for std::list

Is there any major difference between const_iterator and iterator implementations for std::list except operator* and operator->. Below you can see my non-templated homework-style example to show the gist. As much as I understand the rest of the methods just copy each-other for these classes, such as CTOR, operator==, operator!=, operator++, operator++(int), operator--, operator--(int).

class iterator
{
private:
    Node* m_node;

public:
    iterator(Node* node)
        : m_node(node)
    {

    }

    int& operator*()
    {
        return m_node->value;
    }

    Node* operator->()
    {
        return m_node;
    }

 ....
}

Now const iterator implementation

class const_iterator
{
private:
    Node* m_node;

public:
    const_iterator(Node* node)
        : m_node(node)
    {

    }

    int operator*() const
    {
        return m_node->value;
    }

    const Node* operator->() const
    {
        return m_node;
    }
......
}

Aucun commentaire:

Enregistrer un commentaire