Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class String {
- public:
- String():rep(new StringRep("")) { }
- String(const String &s): rep(s.rep) { rep->count++; }
- String &operator=(const String &s){
- String(s).swap(*this); // copy-and-swap idiom
- return *this;
- }
- ~String() { // StringRep deleted only when the last handle goes out of scope.
- if(rep && --rep->count <= 0) delete rep;
- }
- String(const char *s): rep(new StringRep(s)) { }
- void swap (String & s) throw () {
- std::swap(this->rep, s.rep);
- }
- private:
- StringRep *rep;
- };
Add Comment
Please, Sign In to add comment