Thread: Ranged-based for loop using a reference

  1. #1
    Registered User
    Join Date
    Feb 2014
    Posts
    5

    Ranged-based for loop using a reference

    Having read the article on ranged-based for loops here: Range-Based For Loops in C++11 - Cprogramming.com

    I was trying to use Alex's example only using a std::vector instead of an int array in a class named IntVector and also changing the get method to return a reference so when I loop through in main I can modify each element.

    I changed the methods to be non-const like he said but for some reason any modifications I make in main when loop through the range-based loop don't last. I also had to make the Iter class a template as my class to be iterated through is a template.

    My implementation:

    Code:
    #ifndef QUEUETYPE_H
    #define QUEUETYPE_H
    #include <algorithm>
    #include <cstdlib>
    #include <vector>
    using namespace std;
    
    template<typename T> class QueueType;
    
    template<typename T>
    class Iter {
    public:
    
        Iter(QueueType<T> *new_p_Queue, int nextPos)
        : pos(nextPos),
        p_Queue(new_p_Queue) {
        }
    
        bool operator !=(Iter& other) {
            return pos != other.pos;
        }
        T operator*();
        Iter& operator++() {
            ++pos;
            return *this;
        }
    private:
        int pos;
        QueueType<T> *p_Queue;
    };
    
    
    template<typename T>
    class QueueType {
    public:
        QueueType();
        ~QueueType();
        QueueType(const QueueType& other);
    
        T& get(int index) {return v[index];}
        Iter<T> begin() {return Iter<T>(this, 0);}
        Iter<T> end() {return Iter<T>(this, numElements);}
    
        int getNumElements() const {return numElements;}
        T& getFront() {return v.at(0);}
        void addElement(T);
        void removeElement();
    
        bool isEmpty() const {return numElements == 0;}
        bool isFull() const {return SIZE == numElements;}
    
        QueueType<T>& operator=(const QueueType other);
    
        friend void swap(QueueType& first, QueueType& second) {
            using std::swap;
            swap(first.numElements, second.numElements);
            swap(first.v, second.v);
        }
    private:
        static const int SIZE = 25;
        int numElements;
        vector<T> v;
    };
    
    template<typename T>
    T Iter<T>::operator *() {
        return p_Queue->get(pos);
    }
    
    template<typename T>
    QueueType<T>::QueueType() {
        v.reserve(SIZE);
        numElements = 0;
    }
    
    template<typename T>
    QueueType<T>::~QueueType() {
    }
    
    template<typename T>
    QueueType<T>::QueueType(const QueueType& other) :
    v(vector<T> (SIZE)),
    numElements(other.numElements) {
        std::copy(other.v, other.v + SIZE, v);
    }
    
    template<typename T>
    void QueueType<T>::addElement(T newElement) {
        v.push_back(newElement);
        numElements++;
    }
    
    template<typename T>
    void QueueType<T>::removeElement() {
        v.erase(v.begin());
        numElements--;
    }
    
    template<typename T>
    QueueType<T>& QueueType<T>::operator=(const QueueType other) {
        swap(*this, other);
        return *this;
    }
    #endif    /* QUEUETYPE_H */
    Then in main I do this:

    Code:
    QueueType<CustomerType> Line;
    ...other things
    for (CustomerType cust : Line) {
        cust.incrementWaitTime();
    }
    But later on each CustomerType in the Line has an unchanged wait time?
    Last edited by Instinct212; 02-22-2014 at 05:16 PM.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    I am thinking posting the code called by incrementWaitTime() is needed.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    >>for (CustomerType cust : Line)
    You are making a copy of every item in Line. Therefore, changes will not be reflected in the original object. Use a reference, e.g.:
    for (auto & cust : Line)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Feb 2014
    Posts
    5
    Ah that does make sense haha thanks also, out of curiosity, is auto any less efficient than naming the type?

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No, it's only syntactic sugar.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. TurboC based, Graphics based program with weird error!
    By sidx64 in forum C++ Programming
    Replies: 4
    Last Post: 11-18-2012, 12:48 PM
  2. Reference to Reference, Pointer to Reference....
    By hqt in forum C++ Programming
    Replies: 5
    Last Post: 11-13-2011, 06:21 AM
  3. Ranged numbers
    By Desolation in forum Game Programming
    Replies: 8
    Last Post: 07-25-2006, 10:02 PM
  4. Generating ranged randomized numbers
    By Cloud_909 in forum C++ Programming
    Replies: 8
    Last Post: 12-05-2005, 05:42 PM
  5. Replies: 6
    Last Post: 11-10-2005, 01:37 PM

Tags for this Thread