code_junkie

Checklist for writing copy constuctor and assignment operator in C++

Nov 11th, 2011
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.44 KB | None | 0 0
  1. Array(const Array& rhs)
  2. {
  3. mData = NULL;
  4. mSize = rhs.size();
  5. *this = rhs;
  6. }
  7.  
  8. Array& operator=(const Array& rhs)
  9. {
  10. if(this == &rhs)
  11. {
  12. return *this;
  13. }
  14.  
  15. int len = rhs.size();
  16.  
  17. delete[] mData;
  18.  
  19. mData = new T[len];
  20.  
  21. for(int i = 0; i < len; ++i)
  22. {
  23. mData[i] = rhs[i];
  24. }
  25.  
  26. mSize = len;
  27.  
  28. return *this;
  29. }
Add Comment
Please, Sign In to add comment