C++简单实现对象引用计数示例(转)
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++简单实现对象引用计数示例(转)的更多相关文章
- Linus:为何对象引用计数必须是原子的
Linus大神又在rant了!这次的吐槽对象是时下很火热的并行技术(parellism),并直截了当地表示并行计算是浪费所有人时间(“The whole “let’s parallelize” thi ...
- Playmaker全面实践教程之简单的使用Playmaker示例
Playmaker全面实践教程之简单的使用Playmaker示例 简单的使用Playmaker示例 通过本章前面部分的学习,相信读者已经对Playmaker有了一个整体的认识和印象了.在本章的最后,我 ...
- 一个简单的JSP程序示例
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"% ...
- lambda表达式/对象引用计数
★lambda表达式的用法例:I=[(lambda x: x*2),(lambda y: y*3)]调用:for x in I: print x(2)输出:4,6 ★获取对象的引用次数sys.getr ...
- python 对象引用计数增加和减少的情况
对象引用计数增加的情况: 1.对象被创建:x=4 2.另外的别人被创建:y=x 3.被作为参数传递给函数:foo(x) ->会增加2 4.作为容器对象的一个元素:a=[1,x,'33'] 对象 ...
- 简单的Spring Batch示例
使用Spring Batch做为批处理框架,可以完成常规的数据量不是特别大的离线计算. 现在写一个简单的入门版示例. 这里默认大家已经掌握了Spring Batch的基本知识,示例只是为了快速上手实践 ...
- 使用TensorFlow v2张量的一个简单的“hello world”示例
使用TensorFlow v2张量的一个简单的"hello world"示例 import tensorflow as tf # 创建一个张量 hello = tf.constan ...
- [ JS 进阶 ] 基本类型 引用类型 简单赋值 对象引用
ECMAScirpt 变量有两种不同的数据类型:基本类型,引用类型.也有其他的叫法,比如原始类型和对象类型,拥有方法的类型和不能拥有方法的类型,还可以分为可变类型和不可变类型,其实这些叫法都是依据这两 ...
- Object-C内存管理-对象引用计数的特例
看到OC中内存管理这块,其中的引用计数部分,部分10.5上的EBOOK示例已经在10.9上不能运行正确了,比如下面的代码: NSString * str1 = @"string 1" ...
随机推荐
- Python9-面对对象1-day22
def Person(name,blood,aggr,sex): person = { 'name' : name, 'blood':blood, 'aggr': aggr, 'sex' : sex, ...
- PAT Basic 1067
1067 试密码 当你试图登录某个系统却忘了密码时,系统一般只会允许你尝试有限多次,当超出允许次数时,账号就会被锁死.本题就请你实现这个小功能. 输入格式: 输入在第一行给出一个密码(长度不超过 20 ...
- 如何完整反编译AndroidMainfest.xml
下载工具: http://code.google.com/p/android4me/downloads/detail?name=AXMLPrinter.zip&can=2&q= 包名为 ...
- linux 系统备份还原
操作系统或文件备份 tar cvpzf backup.tgz --exclude=/proc --exclude=/lost+found --exclude=/backup.tgz --exclude ...
- luogu1963 [NOI2009]变换序列
#include <iostream> #include <cstring> #include <cstdio> using namespace std; int ...
- HDU 3943 K-th Nya Number
K-th Nya Number Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on HDU. Origina ...
- php删除
<?php$id = $_GET['id'];$db= new Mysqli("localhost","root","root",&q ...
- 九度oj 题目1179:阶乘
题目描述: 输入n, 求y1=1!+3!+...m!(m是小于等于n的最大奇数)y2=2!+4!+...p!(p是小于等于n的最大偶数). 输入: 每组输入包括1个整数:n 输出: 可能有多组测试数据 ...
- OpenJ_Bailian——4115鸣人和佐助(带状态的A*)
鸣人和佐助 Time Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u Submit Status Desc ...
- 转载:CMarkUp使用简介
转载地址:http://blog.csdn.net/jonathandj/article/details/4320725 最近正在研究C++下的XML分析工具CMarkup.初次和XML相遇是基于C# ...