產生一個reference-counted String class 並不困難,但是必須注意許多細節。
比如 copy-on-write
Pointers,References,以及copy-on-Write
考慮以下代碼:
String s1 = "hello";
char *p =&s1[1];
String s2 = s1;
*p ='x'; //同時修改了 s1 s2,這不是用戶想要的
產生一個reference-counted String class 並不困難,但是必須注意許多細節。
比如 copy-on-write
Pointers,References,以及copy-on-Write
考慮以下代碼:
String s1 = "hello";
char *p =&s1[1];
String s2 = s1;
*p ='x'; //同時修改了 s1 s2,這不是用戶想要的