引用计数
来源:互联网 发布:js责任链模式 编辑:程序博客网 时间:2024/06/05 15:57
#include<iostream>
using namespace std;
class String
{
public:
String(const char* str)
:_str(new char[strlen(str)+1])
{
strcpy(_str,str);
_refcount = new int(1);
}
~String()
{
if(--_refcount[0] == 0 && _str != NULL)
{
delete []_refcount;
delete[] _str;
}
}
String(const String& s)
:_str(s._str)
,_refcount(s._refcount)
{
++_refcount[0];
}
String& operator=(const String& s)
{
if(this != &s)
{
_str = s._str;
_refcount = s._refcount;
++_refcount[0];
}
return *this;
}
public:
char* _str;
int* _refcount;//引用计数
};
int main()
{
String s1("abcd");
String s2(s1);
String s3 = s1;
s3 = s2;
return 0;
}
0 0
- 引用计数
- 引用计数
- 引用计数
- 引用计数
- 引用计数
- 引用计数
- 引用计数
- 引用计数
- 引用计数
- 引用计数
- 引用计数
- 引用计数
- 引用计数
- 引用计数
- 引用计数
- 引用计数
- 引用计数
- 引用计数
- 用Ambari安装hdp2.4错误汇总
- static修饰类,变量和方法,代码块
- JavaScript图的基本构建
- C# Linq 集合作为查询条件获取数据
- 2013山东省第四届ACM省赛 Rescue The Princess
- 引用计数
- C#拖拽文件并显示
- 关于Android开发中启动应用但桌面不显示图标的问题
- 如何将一个字符串的文件直接转换成NSURL
- 从数据库读取前10条数据
- django学习日志(参考了网上各种资料和自己总结,可能存在一些错误,请指正!)
- Scrapy设置之Crawling style
- [Java学习]第一个helloword程序
- 设置转场动画的方向