mardi 25 juillet 2017

Segmentation fault with my Move assignment opertaor overloading

I defined a class

.h

template <typename E,typename I=bool>
class List{
public:
  // internal enum
  enum OrderStatus {decrease =-1,no = 0,increase = 1};

  // Destructor
  ~List();
  // default constructor
  List(OrderStatus ord = no);
  // !!!!!!move assignment operator!!!!!
  List<E,I>& operator=(List<E,I>&& src);

private:
  int size;
  Node<E>* lead; ///boundary sentinel: dummy head
  OrderStatus ordSta;
};

The definition of move assignment operator

template<typename E,typename I>
List<E,I>&
List<E,I>::operator=(List<E,I>&& src){
  // Release any resources *this owns and Reset *this
  Node<E>* cur = lead->next;
  Node<E>* nex = cur->next;
  while(!out(cur)){
    delete cur;
    cur = nex;
    nex = nex->next;
  }
  lead->next = lead->prev = lead;
  size = 0;
  ordSta = no;

  // move
  size = src.size;
  ordSta = src.ordSta;
  lead = src.lead;

  // Reset source object
 src.size = 0;
 src.lead = NULL;
 src.ordSta = no;
 cerr << "bottom" << endl;
 return *this;
}

And I execute following code

int main()
{
  List<int> beta;
  List<int>::OrderStatus tmp = List<int>::increase;
  beta = List<int>(tmp);
  return 0;
}

and get

bottom
Segmentation fault

I just don't know where goes wrong. (Note: I have defined a "foo" template class and try to work with move assignment operator in my environments, and it works)

Aucun commentaire:

Enregistrer un commentaire