Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm having trouble wrapping my head around the concept of an assignment operator, or at least creating them successfully.

Copy Constructors aren't an issue for me; here is mine that its working:

//copy constructor
Set::Set(const Set &rhs){
    _head = rhs._head;
    _tail = rhs._tail;

    //null, basically this object is 0
    if(rhs._head == NULL){
        _head = NULL;
        _tail = NULL;
        _size = 0;
    }else{
        _head = new Elem(*rhs._head);
        _tail = new Elem(*rhs._tail);
        _size = rhs._size;

        Elem *prev = NULL;
        Elem *curr = _head;
        Elem *otherCurr = rhs._head;
        int counter = 0;
        while(otherCurr->next != NULL){
            curr->next = new Elem(*otherCurr->next);
            curr->next->prev = curr;


            curr = curr->next;
            otherCurr = otherCurr->next;
        }

        //now that we are done lets setup the tail
        _tail->prev = curr;
        curr->next = _tail;

    }

}

I was reading example code, and saw some people used the #include <algorithm> library to implement it. I tried that instead, however does not seem to work at all.

//assignment operator
Set& Set::operator=(const Set &rhs){
    Set temp(rhs);
       std::swap(temp._head,_head);
       std::swap(temp._tail, _tail);
       return *this;

}

This above code however does not work correctly. Really struggling to grasp the concept of how assignment operators are made. I figured it would basically work the same as you want to copy values from one into another. But evidently not. If anyone could advise me on how to get that working it would be great.

Just some more general info on my class, there is a _head and a _tail that points to the start and end of the list. Dummy Elements.

Here is how the object is structured:

  struct Elem {
        ELEMENT_TYPE info;
        Elem *prev, *next;
    };
    Elem *_head, *_tail;
    int _size;
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
165 views
Welcome To Ask or Share your Answers For Others

1 Answer

I see two problems with your copy constructor (and not even trying to dig further):

  1. What is the purpose of tail? It doesn't seem to be needed, or is not used correctly.
  2. Your copy loop seems to stop on otherCurr->next being NULL. But you point curr->next on the last element to tail. And this very likely means that you either copy 1 element past your set or even worse (as you may not initialize tail correctly looking at its definition), you are trying to copy random location which will cause your program to crash sooner or later.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...