利用”引用计数”和”写时复制”这两个特点来实现一个字符串类,为了更好地隐藏类的实现,避免出现下述情况:当对于内联函数定义的改动,对于对象成员大小的改动等导致程序的重新编译,我们可以的代码结构如下:
我们在程序中维护两个不同的头文件,在提供给用户使用的那个公共头文件中,只是告诉用户我们定义了String_ref这个类,并且在String对象中用一个指针指向它
Code
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->#ifndef STRING_H
#define STRING_H
#include <iostream>
using namespace std;
#include <assert.h>
class String_ref; //前向声明代理类
//////////////////////////////////////
// String类声明
//////////////////////////////////////
//简单的引用计数型字符串类
class String
{
private:
String_ref* proxy; //代理对象指针
public:
String(const char* = "");
String(const String& s);
~String();
const String& operator = (const String&);
void ToLower();
int Length()const;
char operator[](int)const;
friend ostream& operator << (ostream& out,const String& rhs);
};
#endif
另一个头文件String_priv.h是一个私有的头文件,我们并不需要将其分发给用户,它里面包含了对String_ref类的完整声明:
其对应的实现文件String_priv.cpp:
Code
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->
#include "String_priv.h"
//////////////////////////////////////
// String_ref实现区
//////////////////////////////////////
String_ref::String_ref(const char * cp):count(0),length(0)
{
this->init(cp);
}
String_ref::~String_ref()
{
this->release();
}
String_ref::String_ref(const String_ref& rhs):count(0)
{
this->init(rhs.data);
}
const String_ref& String_ref::operator = (const String_ref& rhs)
{
if(&rhs!=this)
{
delete [] this->data;
init(rhs.data);
}
return *this;
}
char String_ref::operator[] (int index)const
{
assert(index>=0&&index<strlen(this->data));
return this->data[index];
}
int String_ref::Length()const
{
return this->length;
}
void String_ref::AddRef()
{
++this->count;
}
void String_ref::DecRef()
{
if(--this->count==0)
{
this->release();
}
}
void String_ref::init(const char* cp)
{
this->length = strlen(cp);;
this->data = new char[length+1];
strcpy_s(this->data,length+1,cp);
}
void String_ref::release()
{
delete [] this->data;
this->data = NULL;
}
const char* String_ref::getData()const
{
return this->data;
}
//////////////////////////////////////
// String实现区
//////////////////////////////////////
String::String(const char* cp):proxy(new String_ref(cp))
{
proxy->AddRef();
}
String::~String()
{
proxy->DecRef();
}
String::String(const String& s):proxy(s.proxy)
{
proxy->AddRef();
}
const String& String::operator = (const String& s)
{
if(&s!=this)
{
proxy->DecRef();
proxy = s.proxy;
proxy->AddRef();
}
return *this;
}
void String::ToLower()
{//写时复制
if(this->proxy->count>1)
{//还有其他的持有者,进行"写时复制"
String_ref* new_ref = new String_ref(this->proxy->data);//创建新的字符串内部代理对象,拷贝当前字符串到新创建对象
this->proxy->DecRef();//退出原来的
this->proxy = new_ref;
this->proxy->AddRef();//进入新创建的
}
assert(proxy->count==1);//验证自己是创建者
//小写化
for(char* cp=proxy->data;*cp;++cp)
{
*cp = tolower(*cp);
}
}
char String::operator[] (int index)const
{
return (*proxy)[index];
}
int String::Length()const
{
return proxy->Length();
}
ostream& operator << (ostream& out,const String& rhs)
{
out<<rhs.proxy->getData()<<endl;
return out;
}
Code
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->//////////////////////////////////////
// String_ref类声明
//////////////////////////////////////
#include "String.h"
class String_ref
{
private:
friend class String;//友元类
int count;//引用计数
char* data;//数据域
public:
String_ref(const char*);
String_ref(const String_ref&);
~String_ref();
const String_ref& operator = (const String_ref&);
char operator[](int)const;
int Length()const;
void AddRef();//引用计数加1
void DecRef();//引用计数减1
const char* getData()const;
private:
void init(const char*);
void release();
};
测试代码:
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->#include"String.h"
intmain()
{
Strings1("HelloWorld");
Strings2(s1);
Strings3;
s3=s1;
cout<<s1<<s2<<s3;
s1.ToLower();
cout<<s1<<s2<<s3;
cout<<s1[0]<<endl;
cout<<s1.Length()<<endl;
system("pause");
return0;
}
分享到:
相关推荐
引用计数的实现,c++源码实现,工程可编译,利用模板实现的,项目中可直接使用。部分代码是从webkit中引用计数直接引用过来的
该demo主要展现智能指针和引用计数的实现过程,总共两个类,一个智能指针类,一个引用计数类,另外附一份执行步骤资料
COM入门学习实例,c++程序实现引用计数易于理解,动态绑定相关
COM笔记-引用计数 源码 vc6.0 平台
脉冲计数型鉴频器是利用计过零点脉冲数目的方法实现的,所以叫做脉冲计数式鉴频器。它是将调频波变换为重复频率等于调频波频率的等幅等宽脉冲序列,再经低通滤波器取出直流平均分量。 调频信号经限幅加到形成级进行...
ios5的内存管理引入自动引用计数,本资料进行了详细说明和介绍,供有需要的人下载使用。
Delphi结构中使用String时遇到的内存泄露问题(没有利用String的引用计数自动销毁字符串的功能) .mht
MyString 字符串类仿写_C++_(四种版本,引用计数,迭代器,加锁)
智能指针相信大家听说过吧,我理解的智能指针,既是一个C++模板类,重载了指针操作符(->)和(*)操作符,而使它的实例可以"用指针的形式...可以实现任意类型(基本数据类型、自定义类型)的指针地址的自动引用计数。
计量型测量系统分析与计数型测量系统分析.pptx
【车辆计数】基于光流法实现车辆检测计数matlab 源码.md
单片机C语言程序设计33 用计数器中断实现100以内的按键计数(基于8051+Proteus仿真)单片机C语言程序设计33 用计数器中断实现100以内的按键计数(基于8051+Proteus仿真)单片机C语言程序设计33 用计数器中断实现100...
C++实现 带引用计数的智能指针 VS2008工程文件
论文研究-计数型序贯网图检验的算法实现.pdf, 在深入分析序贯网图检验方法的基础上, 通过构造马氏链模型,给出其实际风险的计算方法, 给出了序贯网图检验方案的制定方法....
给易语言的类使用引用计数。继承object类之后,。a=b 赋值的的时候 a和b指向同一个对象。可以在方法返回自身,达到连续调用类的方法修改同一个对象的目的。@si。
配网自动化及其实现方式(电压时间型、电流计数型工作原理)(PPT83页).ppt
采用引用计数的智能指针share_ptr,可以用于标准库容器
基于Multisim14,绘制的JK触发器及D触发器构成计数型触发器仿真.
C++引用计数设计与分析(解决垃圾回收问题).docx
本Delphi示例用于说明动态数组的引用计数机制基于变量而非元素。输出结果是这样的: ______________________________________________________ aba aba aba aaa ______________________________...