首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > C/C++ > 编程交流与学习--More Effective C++的学习-Item M29:引用计数
【标  题】:编程交流与学习--More Effective C++的学习-Item M29:引用计数
【关键字】:--More,Effective,C++,-Item,M29
【来  源】:http://blog.csdn.net/swordll80/archive/2006/10/26/1352679.aspx

编程交流与学习--More Effective C++的学习-Item M29:引用计数

引用计数是这样一个技巧,它允许多个有相同值的对象共享这个值的实现。引用计数是个简单的垃圾回收体系。

l        实现引用计数

class String {

public:

  ...   // the usual String member functions go here

private:

  struct StringValue { ... }; // holds a reference count and a string value

  StringValue *value; // value of this String

};

这是StringValue的实现:

class String {

private:

    struct StringValue {

      int refCount;

      char *data;

      StringValue(const char *initValue);

      ~StringValue();

    };

    ...

};

String::StringValue::StringValue(const char *initValue): refCount(1)

{  data = new char[strlen(initValue) + 1];

  strcpy(data, initValue);}

String::StringValue::~StringValue()

{  delete [] data;}

StringValue的主要目的是提供一个空间将一个特别的值和共享此值的对象的数目联系起来。

String构造函数:

String::String(const char *initValue)

: value(new StringValue(initValue))

{}

String::String(const String& rhs)

: value(rhs.value)

{

  ++value->refCount;

}

String析构函数:

String::~String()

{

  if (--value->refCount == 0) delete value;

}

赋值操作:

String& String::operator=(const String& rhs)

{

  if (value == rhs.value) {          // do nothing if the values

    return *this;                    // are already the same; this

  }                                  // subsumes the usual test of

                                     // this against &rhs (see Item E17)

  if (--value->refCount == 0) {      // destroy *this's value if

    delete value;                    // no one else is using it

  }

  value = rhs.value;                 // have *this share rhs's

  ++value->refCount;                 // value

  return *this;

}

l        写时拷贝

char& String::operator[](int index)

{

  // if we're sharing a value with other String objects,

  // break off a separate copy of the value for ourselves

  if (value->refCount > 1) {

    --value->refCount;                    // decrement current value's

                                          // refCount, because we won't

                                          // be using that value any more

    value =                               // make a copy of the

      new StringValue(value->data);       // value for ourselves

  }

  // return a reference to a character inside our

  // unshared StringValue object

  return value->data[index];

}

这个“与其它对象共享一个值直到写操作时才拥有自己的拷贝”的想法在计算机科学中已经有了悠久而著名的历史了,尤其是在操作系统中:进程共享内存页直到它们想在自己的页拷贝中修改数据为止。这个技巧如此常用,以至于有一个名字:写时拷贝。它是提高效率的一个更通用方法--Lazy原则--的特例。

原文讨论的其它问题,略。

 
c++动态建立学生排序范例:【上一篇】
hanoi:【下一篇】
【相关文章】
  • c++动态建立学生排序范例
  • 从笑话中悟出C++开发管理之"道"
  • 编程交流与学习--More Effective C++的学习-Item M25:将构造函数和非成员函数虚拟化
  • Thinking in C++(3)
  • 1.3 利用Visual C++/MFC开发Windows程序的优势
  • 用Visual C++干干净净地清除进程
  • 实例解析C++/CLI程序进程之间的通讯
  • 软件外企C++面试题,大家试试看
  • C++&Lisp比较
  • 孙鑫VC++讲座笔记-(3)MFC程序框架的剖析
  • 【随机文章】
  • LiveMotion完全教程(目录)
  • NT下基于邮件服务软件(IMAIL)的邮件发送程序--(本地版)
  • 实现最佳商业价值 实战Office 2003共享表单
  • Solaris网络管理
  • EWF概述
  • 谈谈Java编程学习与求职---怎样学习Java (心得分享篇)
  • 可以显示和修改 HTML 文件
  • SQL数据排序
  • 三方协议,体检,毕业设计,论文
  • 不要把所有鸡蛋都放在一个篮子里——使用RoboCopy实现网络备份
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 软讯网络 All Rigths Reserved.