Thread: correct behavour of at()

  1. #1
    Registered Abuser
    Join Date
    Sep 2007
    Location
    USA/NJ/TRENTON
    Posts
    127

    correct behavour of at()

    I wanted to build this list ground up again. I'd like to know if I'm on a better track this time?

    So far, my example really only tests Iter creation/copying/assigning.


    1) Is my Iter assignment operator correct?

    2) Is this how the funtion at() is normally implemented?

    AList.h:
    Code:
    #ifndef ALIST_H
    #define ALIST_H
    
    template <typename T>
    class List{
        
        private:
          
            struct Node{
                
                T data;
                Node *next, *prev;
                Node(const T& d, Node* n = 0, Node* p = 0) : data(d), next(n), prev(p) {};
                Node(const Node& src) : data(src.data), next(src.next), prev(src.prev) {};
            };
            
        Node *head, *tail;    
            
        unsigned int sizeOfList;
            
        public:
            
            class Iter{
                
                private:
                    
                    Node* curr;
                    
                public:
                    
                    Iter(Node* c = 0) : curr(c) {};
                    Iter(const Iter& rhs_i) : curr(rhs_i.curr) {};
                    
                    Iter& operator=(const Iter& rhs_i){ Node* c = rhs_i.curr; return *this; };
                    
                    const T& operator*() const { return curr->data; };
                    T& operator*() { return curr->data; };
                    
                    bool operator!=(const Iter& i) { return i.curr != curr; };
                    bool operator==(const Iter& i) { return i.curr == curr; };
                    
                    Iter& operator++(int) { curr = curr->next; return *this; };
                    Iter& operator--(int) { curr = curr->prev; return *this; };
                    
            };
            
        List<T>();
        List<T>(const List& src);
        unsigned int size(){ return sizeOfList; };
        
        const Iter begin() const { return Iter(head); };
        const Iter end() const { return Iter(0); };
        const Iter rbegin() const { return Iter(tail); };
        const Iter rend() const { return Iter(0); };
        
        
        /*  Here's at()
        */
        const Iter at(int n) const {
        
        	Iter temp = begin();
    			for(int i = 0;  i < n; i++){
    				
    				temp++;
    			}
    			
    			return temp;
            }
            
    };
    
    template <typename T>
    List<T>::List() : head(0), tail(0), sizeOfList(0) {}
    
    #endif
    AMain.cpp:
    Code:
    #include <iostream>
    #include "AList.h"
    
    int main(){
        
        List<int> mlist;
        
        List<int>::Iter i;
        List<int>::Iter j(i);
        List<int>::Iter k;
        List<int>::Iter l = k = j;
        
        return 0;
    }

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
     Iter& operator=(const Iter& rhs_i){ Node* c = rhs_i.curr; return *this; };
    You are assigning to a temporary.

    I don't think either Node nor Iter needs a special copy contructor or assignment operator. They don't require anything else but the shallow copy that the compiler would produce automatically.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You have incorrectly implemented postincrement as if it were preincrement, so the semantics of it is wrong.

    There should be no "at" function for a list. Any O(n) running time list accessor is fundamentally wrong, as it encourages writing horribly inefficient code. This is why the SC++L does not have it for a list.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I think Node should be noncopyable. It doesn't make sense to copy nodes.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered Abuser
    Join Date
    Sep 2007
    Location
    USA/NJ/TRENTON
    Posts
    127
    Quote Originally Posted by anon
    I don't think either Node nor Iter needs a special copy contructor or assignment operator. They don't require anything else but the shallow copy that the compiler would produce automatically.
    Quote Originally Posted by iMalc
    There should be no "at" function for a list. Any O(n) running time list accessor is fundamentally wrong, as it encourages writing horribly inefficient code. This is why the SC++L does not have it for a list.
    So on both accounts just remove and implement w/out?

  6. #6
    Registered Abuser
    Join Date
    Sep 2007
    Location
    USA/NJ/TRENTON
    Posts
    127
    Quote Originally Posted by CornedBee
    I think Node should be noncopyable. It doesn't make sense to copy nodes.
    Is there a special way of preventing the copying of Nodes?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Make the copy constructor and copy assignment operator private.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by sh3rpa View Post
    So on both accounts just remove and implement w/out?
    yep, yep.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    AAAck.. I would not provide an at() function for a list. You run the risk of somebody attempting to use it!

    EDIT: Seems I was beaten to the punch.

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by laserlight View Post
    Make the copy constructor and copy assignment operator private.
    And don't implement them.


    Alternatively, derive from boost::noncopyable.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linux for GNU/Linux is not correct?
    By password636 in forum Linux Programming
    Replies: 8
    Last Post: 03-31-2009, 08:30 PM
  2. Is this correct : passing strings?
    By socket in forum C Programming
    Replies: 15
    Last Post: 11-25-2008, 02:03 PM
  3. Replies: 1
    Last Post: 05-26-2004, 12:58 AM
  4. Why I couldn't see the correct format of a file?
    By miketv in forum C Programming
    Replies: 2
    Last Post: 01-23-2002, 10:59 PM
  5. Check if input password is correct
    By kagemand in forum C++ Programming
    Replies: 2
    Last Post: 11-28-2001, 09:28 AM