Guest User

Untitled

a guest
Jul 21st, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.61 KB | None | 0 0
  1. class String {
  2. public:
  3. String():rep(new StringRep("")) { }
  4. String(const String &s): rep(s.rep) { rep->count++; }
  5. String &operator=(const String &s){
  6. String(s).swap(*this); // copy-and-swap idiom
  7. return *this;
  8. }
  9. ~String() { // StringRep deleted only when the last handle goes out of scope.
  10. if(rep && --rep->count <= 0) delete rep;
  11. }
  12. String(const char *s): rep(new StringRep(s)) { }
  13. void swap (String & s) throw () {
  14. std::swap(this->rep, s.rep);
  15. }
  16. private:
  17. StringRep *rep;
  18. };
Add Comment
Please, Sign In to add comment