Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++: const_iterator and the for-each loop

I am making my own implementation of Vector in C++. This is a two part question.

Part 1: When attempting to iterate through a Vector, my begin() and end() iterators are unable to handle the input arguments. Below is my entire Vector.h implementation:

    class iterator{
    public:

        typedef std::random_access_iterator_tag iterator_category;
        typedef T value_type;

        Vector<T>* ptr;
        uint64_t position;
        COMLINK slave;

        void init(void){
            ptr = nullptr;
            position = 0;
        }
        void copy(const iterator& i){
            init();
            ptr = i.ptr;
            position = i.position;
            slave = i.slave;
        }
        void move(iterator&& i){
            init();
            std::swap(ptr, i.ptr);
            std::swap(position, i.position);
            std::swap(slave, i.slave);
        }

        iterator(void){
            init();
        }
        iterator(const iterator& q){
            copy(q);
        }
        explicit iterator(Vector<T>* v, uint64_t offset){
            init();
            ptr = v;
            position = offset;
            slave = v->master;
        }
        T& operator*(void){
            if ((position >= ptr->len) || ((ptr->buffer + position) < ptr->elem))throw invalid_iterator(invalid_iterator::SEVERE);
            else if (slave.move_cnt != ptr->master.move_cnt) throw invalid_iterator(invalid_iterator::MODERATE);
            else if (slave.alter_cnt != ptr->master.alter_cnt) throw invalid_iterator(invalid_iterator::MILD);
            else if (slave.position_cnt != ptr->master.position_cnt) throw invalid_iterator(invalid_iterator::WARNING);
            return *(ptr->elem + position);
        }
        bool operator==(const iterator& rhs){
            return (ptr == rhs.ptr) && (position == rhs.position) && (slave == rhs.slave);
        }
        bool operator!=(const iterator& rhs){
            return !((*this) == rhs);
        }
        iterator& operator=(const iterator& rhs){
            copy(rhs);
            return *this;
        }
        iterator& operator++(void){
            position++;
            return *this;
        }
        iterator& operator--(void){
            position--;
            return *this;
        }
        iterator operator+(uint64_t i){ // p + i
            iterator temp(*this);
            temp.position += i;
            return temp;
        }
        iterator operator-(uint64_t i){ // p - i
            iterator temp(*this);
            temp.position -= i;
            return temp;
        }
        uint64_t operator-(const iterator& q){
            return position - q.position;
        }
    };

    class const_iterator : public iterator {
    public:
        Vector<T>* const ptr;

        void init(void){
            ptr = nullptr;
            position = 0;
        }
        void copy(const_iterator& i){
            init();
            ptr = i.ptr;
            position = i.position;
            slave = i.slave;
        }
        void copy(iterator& i){
            init();
            ptr = i.ptr;
            position = i.position;
            slave = i.slave;
        }
        void move(const_iterator&& i){
            init();
            std::swap(ptr, i.ptr);
            std::swap(position, i.position);
            std::swap(slave, i.slave);
        }
        void move(iterator&& i){
            init();
            std::swap(ptr, i.ptr);
            std::swap(position, i.position);
            std::swap(slave, i.slave);
        }

        const_iterator(void){
            init();
        }
        const_iterator(const_iterator& i){
            copy(i);
        }
        explicit const_iterator(Vector<T>* const v, uint64_t offset){
            init();
            ptr = v;
            position = offset;
            slave = v->master;
        }
        const_iterator(iterator& i){
            copy(i);
        }
        const T& operator*(void){
            if ((position >= ptr->len) || ((ptr->buffer + position) < ptr->elem))throw invalid_iterator(invalid_iterator::SEVERE);
            else if (slave.move_cnt != ptr->master.move_cnt) throw invalid_iterator(invalid_iterator::MODERATE);
            else if (slave.alter_cnt != ptr->master.alter_cnt) throw invalid_iterator(invalid_iterator::MILD);
            else if (slave.position_cnt != ptr->master.position_cnt) throw invalid_iterator(invalid_iterator::WARNING);
            return *(ptr->elem + position);
        }
        const_iterator& operator=(iterator& i){
            copy(i);
            return *this;
        }
    };

And these are the beign() and end() functions in vector:

    iterator& begin(){
        return iterator(this, 0);
    }

    const_iterator& begin() const{
        return const_iterator(this, 0);
    }

    iterator& end(){
        return iterator(this, len);
    }

    const_iterator& end() const{
        return const_iterator(this, len);
    }

And... finally (sorry for length) ... here is the test code triggering the compile error:

const Vector<int32_t>& y = x;
int32_t s = 0;
for (const auto& v : y) {
    s += v;
}

The error I receive is:

Warning 3   warning C4172: returning address of local variable or temporary c:\users\alexander\documents\visual studio 2013\projects\vectorcontainerphasec_debug\vector.h   319 1   VectorContainerPhaseC_Debug
Error   4   error C2665: 'epl::Vector<int>::const_iterator::const_iterator' : none of the 4 overloads could convert all the argument types  c:\users\alexander\documents\visual studio 2013\projects\vectorcontainerphasec_debug\vector.h   323 1   VectorContainerPhaseC_Debug

I have been researching this issue for hours and can't find a solution. Any tips?

Part 2: Is there another way to implement const_iterator than I have done above? It seems redundant for me to redefine so many functions from iterator to const_iterator. Do I even need to create a const_iterator class?

like image 670
alex Avatar asked Oct 21 '22 13:10

alex


1 Answers

You probably want your const_iterator to point to a const Vector. You also need to adjust your constructor, like follows:

class const_iterator : public iterator {
public:
    const Vector<T>* const ptr;

    ...
    explicit const_iterator(const Vector<T>* v, uint64_t offset){
        ...
    }

That way, your const_iterator constructor should be callable within your range for iteration.
See Difference between const declarations in C++ if you need to understand the details of how the const keyword works in pointer declarations.

like image 54
Martin J. Avatar answered Oct 23 '22 03:10

Martin J.