String类的写时拷贝实例
来源:本站原创|时间:2020-01-10|栏目:C语言|点击: 次
实例如下:
#include<iostream> using namespace std; class String; ostream& operator<<(ostream &out, const String&s); //引用计数器类 class String_rep { friend class String; friend ostream& operator<<(ostream &out, const String&s); public: String_rep(const char *str ) :use_count(0) { if (str == NULL) { data = new char[1]; data[0] = '\0'; } else { data = new char[strlen(str) + 1]; strcpy(data, str); } } String_rep(const String_rep &rep) :use_count(0) { data = new char[strlen(rep.data) + 1]; strcpy(data, rep.data); } String_rep& operator=(const String_rep &rep) { if (this != &rep) { delete[]data; data = new char[strlen(rep.data) + 1]; strcpy(data, rep.data); } return *this; } ~String_rep() { delete[]data; data = NULL; } public: void increase() { ++use_count; } void decrease() { if (use_count == 0) { delete this; //自杀行为 释放this所指的空间,在释放之前调动这个类的析构函数 } } private: char *data; int use_count; }; //////////////////////////////////////////////////////////////////////////////////////// class String { friend ostream& operator<<(ostream &out, const String&s); public: String(const char* str = " ") { rep = new String_rep(str); rep->increase(); } String(const String &s) { rep = s.rep; //浅拷贝 rep->increase(); } String& operator=(const String &s) { if (this != &s) { rep->decrease(); //模拟delete rep = s.rep; //模拟new rep->increase(); //模拟strcpy /*rep = s.rep; //这会更改引用计数器指针 ,造成s内存泄漏 rep->increase();*/ } return *this; } ~String() { rep->decrease(); } public: void to_upper() { if (rep->use_count > 1) { String_rep* new_rep = new String_rep(rep->data); rep->decrease(); rep = new_rep; rep->increase(); } char* ch = rep->data; while (*ch != '\0') { *ch -= 32; ++ch; } } private: String_rep *rep; //引用计数器 }; ostream& operator<<(ostream &out, const String&s) { out << s.rep->data; return out; } void main() { String s1("hello"); String s2(s1); String s3; s3 = s2; cout << "s1=" << s1 << endl; cout << "s2=" << s2 << endl; cout << "s3=" << s3 << endl; s2.to_upper(); cout << "-----------------------------------------------" << endl; cout << "s1=" << s1 << endl; cout << "s2=" << s2 << endl; cout << "s3=" << s3 << endl; }
以上这篇String类的写时拷贝实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。
您可能感兴趣的文章
- 04-02c语言的正则匹配函数 c语言正则表达式函数库
- 04-02c语言中对数函数的表达式 c语言中对数怎么表达
- 04-02C语言中怎么打出三角函数 c语言中怎么打出三角函数的值
- 01-10c语言求1+2+...+n的解决方法
- 01-10求子数组最大和的解决方法详解
- 01-10深入理解约瑟夫环的数学优化方法
- 01-10深入二叉树两个结点的最低共同父结点的详解
- 01-10数据结构课程设计- 解析最少换车次数的问题详解
- 01-10c语言 跳台阶问题的解决方法
- 01-10如何判断一个数是否为2的幂次方?若是,并判断出来是多少次方
![](/a1/style/images/004af07a5ae19f0be5b1cbbbec88694e.png)
![](/a1/style/images/212000e21aa0a880f293fc28bb750a01.png)
阅读排行
本栏相关
- 04-02c语言函数调用后清空内存 c语言调用
- 04-02func函数+在C语言 func函数在c语言中
- 04-02c语言的正则匹配函数 c语言正则表达
- 04-02c语言用函数写分段 用c语言表示分段
- 04-02c语言中对数函数的表达式 c语言中对
- 04-02c语言编写函数冒泡排序 c语言冒泡排
- 04-02c语言没有round函数 round c语言
- 04-02c语言分段函数怎么求 用c语言求分段
- 04-02C语言中怎么打出三角函数 c语言中怎
- 04-02c语言调用函数求fibo C语言调用函数求
随机阅读
- 01-10delphi制作wav文件的方法
- 01-10C#中split用法实例总结
- 01-10SublimeText编译C开发环境设置
- 01-11Mac OSX 打开原生自带读写NTFS功能(图文
- 04-02jquery与jsp,用jquery
- 01-10使用C语言求解扑克牌的顺子及n个骰子
- 08-05织梦dedecms什么时候用栏目交叉功能?
- 08-05DEDE织梦data目录下的sessions文件夹有什
- 08-05dedecms(织梦)副栏目数量限制代码修改
- 01-11ajax实现页面的局部加载