Effective C++ 一个简单的 String 类的 Reference Counting 实现(C++)
来源:互联网 发布:淘宝抢购软件 编辑:程序博客网 时间:2024/06/06 01:54
// Reference-counting pointertemplate<class T>class RCPtr{public:RCPtr(T* realPtr = nullptr);RCPtr(const RCPtr& rhs);~RCPtr();RCPtr& operator = (const RCPtr& rhs);T* operator-> () const;T& operator* () const;private:T* pointee;void init();};template<class T>void RCPtr<T>::init(){if (pointee == nullptr) return;if (pointee->isShareable() == false){pointee = new T(*pointee);}pointee->addReference();}template<class T>RCPtr<T>::RCPtr(T* realPtr): pointee(realPtr){init();}template<class T>RCPtr<T>::RCPtr(const RCPtr& rhs): pointee(rhs.pointee){init();}template<class T>RCPtr<T>& RCPtr<T>::operator = (const RCPtr& rhs){if (pointee != rhs.pointee){if (pointee){pointee->removeReference();}pointee = rhs.pointee;init();}return *this;}template<class T>RCPtr<T>::~RCPtr(){if (pointee){pointee->removeReference();}}template<class T>T* RCPtr<T>::operator -> () const{return pointee;}template<class T>T& RCPtr<T>::operator * () const{return *pointee;}// Reference-counting objectclass RCObject{protected:RCObject();RCObject(const RCObject& rhs);RCObject& operator = (const RCObject& rhs);virtual ~RCObject() = 0;public:void addReference();void removeReference();void markUnshareable();bool isShareable() const;bool isShared() const;private:int refCount;bool shareable;};RCObject::RCObject(): refCount(0), shareable(true){}RCObject::RCObject(const RCObject&): refCount(0), shareable(true){}RCObject& RCObject::operator=(const RCObject&){return *this;}RCObject::~RCObject() {}void RCObject::addReference(){++refCount;}void RCObject::removeReference(){if (--refCount == 0){delete this;}}void RCObject::markUnshareable(){shareable = false;}bool RCObject::isShareable() const{return shareable;}bool RCObject::isShared() const{return refCount > 1;}// String classclass String{public:String(const char* initValue = "");const char& operator [] (int index) const;char& operator [] (int index);friend ostream& operator << (ostream& lhs, const String& rhs);private:struct StringValue : public RCObject{char *data;StringValue(const char* initValue);StringValue(const StringValue& rhs);void init(const char *initValue);~StringValue();};RCPtr<StringValue> value;};void String::StringValue::init(const char* initValue){data = new char[strlen(initValue) + 1];strcpy(data, initValue);}String::StringValue::StringValue(const char* initValue){init(initValue);}String::StringValue::StringValue(const StringValue& rhs){init(rhs.data);}String::StringValue::~StringValue(){delete[] data;}String::String(const char* initValue): value(new StringValue(initValue)){}const char& String::operator [] (int index) const{return value->data[index];}char& String::operator[](int index){if (value->isShared()){value = new StringValue(value->data);}value->markUnshareable();return value->data[index];}ostream& operator << (ostream& lhs, const String& rhs){lhs << rhs.value->data;return lhs;}
0 0
- Effective C++ 一个简单的 String 类的 Reference Counting 实现(C++)
- C++string类的简单实现
- c++string类的简单实现
- C++string类的一个实现
- Effective Objective-C 2.0: Item 29: Understand Reference Counting
- reference counting C complentation
- 【C++】浅拷贝和深拷贝以及怎样实现一个简单的string类!!!
- [C++]String类的实现
- [C++]String类的实现
- [C++]String类的实现
- C++String类的实现
- 【c++】string类的实现
- C++string类的实现
- C++string类的实现
- C++String类的实现
- C++string类的实现
- C/C++string类的实现
- 一个简单的http_server的c实现
- Centos 6.6 安装python3.4及Django1.9
- C++通过ANE发送中文字符串到AS防止乱码的处理
- JS模态窗口返回值兼容问题解决方案
- 成员变量和属性
- GCD
- Effective C++ 一个简单的 String 类的 Reference Counting 实现(C++)
- event.preventDefault()方法的使用
- 剑指offer系列之二十七:数组中出现次数超过一半的数
- 简单实用的 Ubuntu 快捷键
- 读某大型网站技术架构总结
- java EE中的任务定时器,当到某个时间的时候执行相应的操作
- mysql blob字段导入
- Win8.1无法安装msi软件提示2502、2503错误怎么解决?
- 设计模式之访问者模式