简介

引用计数就是对一个对象记录其被引用的次数,其的引用计数可加可减,那什么时候加什么时候减呢?所以引用计数的重点在于在哪里加,在哪里减;

加:

减:

实现

//
// Ref.hpp
// Ref
//
// Created by George on 16/11/6.
// Copyright © 2016年 George. All rights reserved.
// #ifndef Ref_hpp
#define Ref_hpp #include <iostream>
#include <string> template <typename T>
class RefPtr; template <typename T>
class Ref {
private:
Ref(T *ptr);
~Ref(); friend class RefPtr<T>; //定义指针管理类为友元,因为管理类需要直接操作指针类 /**
* 增加引用计数
*/
void ref(); /**
* 减少引用计数
*
* @param count <#count description#>
*/
void unref(); /**
* 返回引用计数的个数
*
* @return <#return value description#>
*/
int getCount(); private:
int count; //引用计数
T *p; //基础对象指针
}; template <typename T>
Ref<T>::Ref(T *ptr) : p(ptr), count(1) { } template <typename T>
Ref<T>::~Ref() {
// count--;
// if (count <= 0) {
// count = 0;
// delete p;
// }
delete p;
} template <typename T>
void Ref<T>::ref() {
count++;
} template <typename T>
void Ref<T>::unref() {
count--;
if (count < 0) {
count = 0;
}
} template <typename T>
int Ref<T>::getCount() {
return count;
} template <typename T>
class RefPtr {
public:
RefPtr(T *ptr);
RefPtr(const RefPtr<T>&);
~RefPtr(); RefPtr& operator=(const RefPtr<T>&);
T & operator*();
T * operator->(); inline std::string getTag() { return _tag; };
void setTag(const std::string value) { _tag = value; }; inline int getRefCount() { return ref->getCount(); } private:
Ref<T> *ref;
std::string _tag;
}; /**
* 构造函数
*/
template <typename T>
RefPtr<T>::RefPtr(T *ptr) : ref(new Ref<T>(ptr)), _tag("") { } /**
* 构造函数,不需要对参数进行计数操作
*/
template <typename T>
RefPtr<T>::RefPtr(const RefPtr<T> & nref) {
ref = nref.ref;
ref->ref();
_tag = "";
} /**
* 销毁函数
*/
template <typename T>
RefPtr<T>::~RefPtr() {
ref->unref();
if (ref->getCount() <= 0) {
delete ref;
}
std::cout << this->getTag() << " leave " << ref->getCount() << "次" << std::endl;
} /**
* 重载操作符=
* 左值引用计数减一,右值引用计数加一
*
* @param rhs <#rhs description#>
*
* @return <#return value description#>
*/
template <typename T>
RefPtr<T> & RefPtr<T>::operator=(const RefPtr<T>& rhs) {
rhs->ref->ref();
ref->unref(); if (ref->getCount() <= 0) {
delete ref;
} ref = rhs->ref; return *this;
} /**
* 重载操作符*
*
* @return <#return value description#>
*/
template <typename T>
T & RefPtr<T>::operator*() {
return *(ref->p);
} /**
* 重载->操作符
*
* @return <#return value description#>
*/
template <typename T>
T * RefPtr<T>::operator->() {
return ref->p;
} #endif /* Ref_hpp */

其中Ref是引用,而RefPtr是对Ref进行操作的封装,在什么时候加减;

测试

//
// main.cpp
// Ref
//
// Created by George on 16/11/6.
// Copyright © 2016年 George. All rights reserved.
// #include <iostream>
#include "Ref.hpp" int main(int argc, const char * argv[]) {
// insert code here...
std::cout << "Hello, World!\n"; int *num = new int(2);
{
RefPtr<int> ptr(num);
{
ptr.setTag("Ptr1");
RefPtr<int> ptr2(ptr);
{
ptr2.setTag("Ptr2"); RefPtr<int> ptr3 = ptr2;
ptr3.setTag("Ptr3");
{
int *nnum = new int(4);
RefPtr<int> ptr4(nnum);
ptr4.setTag("Ptr4"); std::cout << "ptr:" << *ptr << std::endl; *ptr = 20; std::cout << "ptr3:" << *ptr3 << std::endl; std::cout << "ptr1 have " << ptr.getRefCount() << "次" << std::endl;
std::cout << "ptr2 have " << ptr2.getRefCount() << "次" << std::endl;
std::cout << "ptr3 have " << ptr3.getRefCount() << "次" << std::endl;
std::cout << "ptr4 have " << ptr4.getRefCount() << "次" << std::endl;
}
}
}
} return 0;
}

结果如下

C++引用计数的更多相关文章

  1. Objective-C内存管理之引用计数

    初学者在学习Objective-c的时候,很容易在内存管理这一部分陷入混乱状态,很大一部分原因是没有弄清楚引用计数的原理,搞不明白对象的引用数量,这样就当然无法彻底释放对象的内存了,苹果官方文档在内存 ...

  2. swift学习笔记5——其它部分(自动引用计数、错误处理、泛型...)

    之前学习swift时的个人笔记,根据github:the-swift-programming-language-in-chinese学习.总结,将重要的内容提取,加以理解后整理为学习笔记,方便以后查询 ...

  3. C++ 引用计数技术及智能指针的简单实现

    一直以来都对智能指针一知半解,看C++Primer中也讲的不够清晰明白(大概是我功力不够吧).最近花了点时间认真看了智能指针,特地来写这篇文章. 1.智能指针是什么 简单来说,智能指针是一个类,它对普 ...

  4. 【Python】引用计数

    一.概述 要保持追踪内存中的对象,Python使用了引用计数这一简单的技术. 二.引用计数的增减 2.1 增加引用计数 当对象被创建并(将其引用)赋值给变量时,该对象的引用计数被设置为1. 对象的引用 ...

  5. iOS开发--引用计数与ARC

    以下是关于内存管理的学习笔记:引用计数与ARC. iOS5以前自动引用计数(ARC)是在MacOS X 10.7与iOS 5中引入一项新技术,用于代替之前的手工引用计数MRC(Manual Refer ...

  6. Objective-C内存管理之-引用计数

    本文会继续深入学习OC内存管理,内容主要参考iOS高级编程,Objective-C基础教程,疯狂iOS讲义,是我学习内存管理的笔记 内存管理 1 内存管理的基本概念 1.1 Objective-C中的 ...

  7. 第3月第2天 find symbolicatecrash 生产者-消费者 ice 引用计数

    1.linux find export find /Applications/Xcode.app/ -name symbolicatecrash -type f export DEVELOPER_DI ...

  8. swif-自动引用计数

    import UIKit /* class Person { let name: String //强引用 init(name: String) { self.name = name print(&q ...

  9. OC中的自动引用计数

    目录: 1,自动引用计数的定义 2,强引用和弱引用 3,类比手动引用 4,循环引用 5,CoreFoundation 内容: 自动引用计数的定义: (Automatic Reference Count ...

  10. ATL是如何实现线程安全的引用计数和多线程控制的

    ATL是如何实现线程安全的引用计数和多线程控制的 正如标题所示,这是我经常被问到的一个问题,而每次我都从头开始给人说一次,其实说来过程理解起来的确有点复杂. 我们的每一个ATL Server Obje ...

随机推荐

  1. 如何将java代码生成一个bat文件

    java -cp classes;lib/* beans.FileUpload  列出所要带的参数,用空格分开Pause

  2. How to solve GM MDI cannot complete the installation

    Dear Joy, I have a problem using GM MDI diagnostic tool. When I installed it on my laptop, the tool ...

  3. UTF-8编码与Unicode CS2的转换

    /* Convert a UTF-8 string into a UCS-2 array. */ void tcstrutftoucs(const char *str, uint16_t *ary, ...

  4. [转]使用ADO.NET访问Oracle存储过程

    本文转自:http://www.cnblogs.com/datasky/archive/2007/11/07/952141.html 本文讨论了如何使用 ADO.NET 访问 Oracle 存储过程( ...

  5. js 刷新页面自动回到顶部

    <script type="application/x-javascript"> addEventListener("load", function ...

  6. Plugin with id 'android-apt' not found

    解决路径: 1.查看miniSDK最外面的build.gradle 添加这句.具体参考一下demo.谢谢

  7. hadoop命令报错:权限问题

    root用户执行hadoop命令报错: [root@vmocdp125 conf]# hadoop fs -ls /user/ [INFO] 17:50:42 main [RetryInvocatio ...

  8. Java多线程编程总结(学习博客)

    Java多线程编程总结:网址:http://lavasoft.blog.51cto.com/62575/27069/

  9. ubuntu系统安装flashplayer

    打开浏览器,输入adobe flashplayer 进入官方网站,下载Linux 32-bit, 简体中文, Firefox,下载.tar.gz包. 然后点击立即下载.下载之后找到解压该文件夹,找到 ...

  10. 注意事项: Oracle Not Exists 及 Not In 使用

    select value from temp_a awhere a.id between 1 and 100and not exists(select * from temp_b b where a. ...