C++简单实现对象引用计数示例

 #include <iostream>
#include <stdio.h> using namespace std; class String {
public:
String(const char *pdata);
String(const String &rhs);
String &operator = (const String &rhs);
~String(); public: //应该使用private,但为了演示的目的而使用public
//内部类
class StringValue {
public:
StringValue(const char *pdata);
~StringValue();
public:
int refCount;
char *data;
}; StringValue *value;//所有的引用对象均共享唯一一个value,value里面实际存储data和引用次数
}; String::StringValue::StringValue(const char *pdata):refCount() {
data = new char[strlen(pdata) + ];
strcpy(data,pdata);
} String::StringValue::~StringValue() {
delete [] data;
} String::String(const char *pdata) : value(new StringValue(pdata)) {} String::String(const String &rhs) {//要对引用加1
value = rhs.value;
(value->refCount)++;//所有指向同一段data的对象的引用加1
} String &String::operator =(const String &rhs) {
if (value == rhs.value)//注意,不是this == &rhs
return *this;
if(--value->refCount == )
{
printf("operator =[delete value] value->refCount=%d\n",value->refCount);
delete value;
} value = rhs.value;
++(value->refCount); return *this;
} String::~String() {
if ( (--value->refCount) == )
{
printf("~String():[delete value] value->refCount=%d\n",value->refCount);
delete value;
}
else
{
printf("~String(): value->refCount=%d\n",value->refCount);
}
} void test() {
String ss("ssss");
printf("ss.value->refCount=%d\n",ss.value->refCount);
String s1 = ss;
printf("s1.value->refCount=%d\n",s1.value->refCount);
printf("ss.value->refCount=%d\n",ss.value->refCount);
String s2("dddd");
printf("s2.value->refCount=%d\n",s2.value->refCount);
s2 = ss;
printf("s2.value->refCount=%d\n",s2.value->refCount);
printf("ss.value->refCount=%d\n",ss.value->refCount);
} int main() {
test();
}

运行结果如下:

ss.value->refCount=1
s1.value->refCount=2
ss.value->refCount=2
s2.value->refCount=1
operator =[delete value] value->refCount=0
s2.value->refCount=3
ss.value->refCount=3
~String(): value->refCount=2
~String(): value->refCount=1
~String():[delete value] value->refCount=0

转自:http://blog.csdn.net/chinawangfei/article/details/50680574

C++简单实现对象引用计数示例(转)的更多相关文章

  1. Linus:为何对象引用计数必须是原子的

    Linus大神又在rant了!这次的吐槽对象是时下很火热的并行技术(parellism),并直截了当地表示并行计算是浪费所有人时间(“The whole “let’s parallelize” thi ...

  2. Playmaker全面实践教程之简单的使用Playmaker示例

    Playmaker全面实践教程之简单的使用Playmaker示例 简单的使用Playmaker示例 通过本章前面部分的学习,相信读者已经对Playmaker有了一个整体的认识和印象了.在本章的最后,我 ...

  3. 一个简单的JSP程序示例

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"% ...

  4. lambda表达式/对象引用计数

    ★lambda表达式的用法例:I=[(lambda x: x*2),(lambda y: y*3)]调用:for x in I: print x(2)输出:4,6 ★获取对象的引用次数sys.getr ...

  5. python 对象引用计数增加和减少的情况

    对象引用计数增加的情况: 1.对象被创建:x=4 2.另外的别人被创建:y=x 3.被作为参数传递给函数:foo(x)  ->会增加2 4.作为容器对象的一个元素:a=[1,x,'33'] 对象 ...

  6. 简单的Spring Batch示例

    使用Spring Batch做为批处理框架,可以完成常规的数据量不是特别大的离线计算. 现在写一个简单的入门版示例. 这里默认大家已经掌握了Spring Batch的基本知识,示例只是为了快速上手实践 ...

  7. 使用TensorFlow v2张量的一个简单的“hello world”示例

    使用TensorFlow v2张量的一个简单的"hello world"示例 import tensorflow as tf # 创建一个张量 hello = tf.constan ...

  8. [ JS 进阶 ] 基本类型 引用类型 简单赋值 对象引用

    ECMAScirpt 变量有两种不同的数据类型:基本类型,引用类型.也有其他的叫法,比如原始类型和对象类型,拥有方法的类型和不能拥有方法的类型,还可以分为可变类型和不可变类型,其实这些叫法都是依据这两 ...

  9. Object-C内存管理-对象引用计数的特例

    看到OC中内存管理这块,其中的引用计数部分,部分10.5上的EBOOK示例已经在10.9上不能运行正确了,比如下面的代码: NSString * str1 = @"string 1" ...

随机推荐

  1. I2C驱动框架(一)

    参考:I2C子系统之内核中I2C子系统的结构 结合vmlinux.lds和Makefile可确定i2c初始化函数的执行顺序如下: 1./dricer/i2c/i2c-core.c中的函数:i2c_in ...

  2. 哈夫曼树:HDU5884-Sort(队列、哈夫曼树)

    Sort Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) 题目链接:http://ac ...

  3. Django的中间件及WSGI

    什么是中间件? 官方的说法:中间件是一个用来处理Django的请求和响应的框架级别的钩子.它是一个轻量.低级别的插件系统,用于在全局范围内改变Django的输入和输出.每个中间件组件都负责做一些特定的 ...

  4. linux查找和替换命令

    http://blog.csdn.net/imyang2007/article/details/8105499 命令的东西应该多练,熟能生巧.

  5. loj2002 「SDOI2017」序列计数

    水题 #include <iostream> #include <cstring> #include <cstdio> using namespace std; t ...

  6. luogu2526 [SHOI2001]小狗散步

    注意一个景点只能去一次. #include <iostream> #include <cstring> #include <cstdio> #include < ...

  7. Linux基础命令详解-1

    本篇详解的命令有以下30个 1.cd 功能:切换工作目录 参数列表     2.ls 功能:查看目录里的内容 参数列表     3.mv 功能:  移动或重命名文件和目录 参数列表     4.pwd ...

  8. [SQL server] IF ELSE 和 CASE WHEN 的用法

    /*判断一个数如果大于10,按10统计,如果小于0,按0统计*/ --方法a DECLARE @AA INT SET @AA=15 IF @AA>10 SELECT 10 ELSE IF @AA ...

  9. pytorch中torch.unsqueeze()函数与np.expand_dims()

    numpy.expand_dims(a, axis) Expand the shape of an array. Insert a new axis that will appear at the a ...

  10. 紫书第五章训练3 D - Throwing cards away I

    D - Throwing cards away I Given is an ordered deck of n cards numbered 1 to n with card 1 at the top ...