Vous êtes sur la page 1sur 1

#include <iostream> #include <cstring> #include <string> using namespace std; class String { struct Strimp1 { char *str;

int refcnt; Strimp1(const char *s) : refcnt(1) { str = strcpy(new char[strlen(s) + 1], s); } ~Strimp1() { delete [] str; } }; Strimp1 *pimp1; public: String(const char *s="") : pimp1(new Strimp1(s)) {} ~String() { if(--pimp1->refcnt == 0) delete pimp1; } String(const String &s) : pimp1(s.pimp1) { ++pimp1->refcnt; } }; int main() { String s1("hello"); { String s2(s1); } }

Vous aimerez peut-être aussi