本文介绍了参考计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 好的,这段代码编译,链接和执行,但是,如何设置,比如, a spinlock来查询DataBase对象的状态让SmrtPtr 知道对象已被删除了吗?: #pragma一次 类SmrtPtrDB { public: SmrtPtrDB():num(0){} ~SmrtPtrDB(){} void add(){num ++;} void sub(){num--;} int status(){return num;} 私人: int num; }; #pragma一次 #包括SmrtPtrDB.hpp 模板< class T> 类SmrtPtr { public: SmrtPtr< T>(T * obj = 0):ptr(obj){DataBase.add();) SmrtPtr< T>(const SmrtPtr< ; T>& rhs):ptr(new T(*(rhs.ptr))){DataBase.add();} ~SmrtPtr< T> (){delete ptr; PTR = 0; DataBase.sub();} void operator =(T val){* ptr = val;} SmrtPtr< T>& operator =(const SmrtPtr< T>&rhs) { if(this!=& rhs) { 删除ptr; ptr(rhs.ptr); } 否则返回此内容; } int status(){DataBase.status();} T& operator *()const {return * ptr;} T * operator->()const {return ptr;} private: SmrtPtrDB数据库; T * ptr; }; 我很困惑。我实现这个权利吗?谢谢!!!! 解决方案 这是我们当前计算的指针代码: // // counts_ptr - 简单引用计数指针。 // //这是一个非侵入式实现,可以分配额外的 / / int和每个计数对象的指针。 #ifndef __COUNTED_PTR_H #define __COUNTED_PTR_H template< class Xclass counting_ptr { public: typedef X element_type; 显式计数_ptr(X * p = 0)//分配一个新计数器 :itsCounter(0){if(p)itsCounter = new counter(p);} ~counting_ptr() {release();} counting_ptr(const counting_ptr& r)throw() {acquire(r.itsCounter);} counting_ptr& operator =(const counting_ptr& r) { if(this!=& r){ release(); 获得(r.itsCounter); } 返回* this; } 模板< class Y> counting_ptr(const counting_ptr< Y>& r)throw() {acquire(r.itsCounter);} 模板< class Y> counting_ptr& operator =(const counting_ptr< Y>& r) { if(this!=& r){ release() ; 获得(r.itsCounter); } 返回* this; } X& operator *()const throw(){return * itsCounter-> ptr;} X * operator->()const throw(){return itsCounter-> ptr;} X * get()const throw(){return itsCounter? itsCounter-> ptr:0;} bool unique()const throw( ) {return(itsCounter?itsCounter-> count == 1:true);} private: struct counter { counter(X * p = 0,unsigned c = 1):ptr(p),count(c){} X * ptr ; 无符号数; } * itsCounter; void acquire(counter * c)throw() {//递增计数 itsCounter = c; if(c)++ c-> count; } void release() {//递减计数,如果为0则删除 if(itsCounter){ if(--itsCounter-> count == 0){ 删除itsCounter-> ptr; 删除itsCounter; } itsCounter = 0; } } }; #endif 很难理解你想要做什么。 你能写一小段代码更多地描述了你想要实现的?更像是智能指针的测试用例... 很难理解你想要做什么。 你能写一段代码来描述更多关于你想要实现的的内容吗?更像是智能指针的测试用例... 实际上这里是代码;它实现了参考计数(我希望!!!) SmrtPtrDB.hpp #pragma一次 类SmrtPtrDB { public: SmrtPtrDB():num(1){} SmrtPtrDB(const SmrtPtrDB& rhs):num(rhs.num){} ~SmrtPtrDB(){} void add(){num ++;} void sub(){num--;} int status(){return num;} private: int num; }; SmrtPtr.hpp 模板< class T> class SmrtPtr { public: SmrtPtr< T>(T * obj = 0):ptr(obj){} SmrtPtr< T>(const SmrtPtr< T>& rhs):ptr(rhs.ptr){DataBase.add();} ~SmrtPtr< T>( ) { DataBase.sub(); if(DataBase.status()== 0) {删除ptr; cout<< 已删除。;}} 其他cout<< 超出范围。 " << endl; } void operator =(T val){* ptr = val;} void operator =(T * val){ ptr = val;} SmrtPtr< T>& operator =(const SmrtPtr< T>&rhs) { if(this!=& rhs) { 删除ptr; ptr(rhs.ptr); } 否则返回此内容; } int status(){return DataBase.status();} T& operator *()const {return * ptr;} T * operator->()const {return ptr;} 运算符T *()const {return ptr ;} 私人: SmrtPtrDB数据库; T * ptr; }; main.cpp #include< iostream> #include< cstdlib> #include" SmrtPtr.hpp" 使用命名空间std; int main() { { SmrtPtr< intptr(new int); ptr = 5; cout<< * ptr<<结束; { SmrtPtr< intptr2(ptr); ptr2 = 6; cout<< ; * ptr<< endl; cout<< * ptr2<< endl; } cout<< * ptr<<结束; } 系统(暂停); 返回EXIT_SUCCESS; } 它显然有效。你能帮我改进吗?谢谢!!!! OK, this code compiles, links, and executes, but, how do I setup, like,a spinlock to query the DataBase object''s status to let the SmrtPtrknow that the object''s been deleted?: #pragma once class SmrtPtrDB{public:SmrtPtrDB():num(0){}~SmrtPtrDB(){}void add(){num++;}void sub(){num--;}int status(){return num;}private:int num;}; #pragma once#include "SmrtPtrDB.hpp" template<class T>class SmrtPtr{public:SmrtPtr<T>(T* obj=0):ptr(obj){DataBase.add();)SmrtPtr<T>(const SmrtPtr<T>& rhs):ptr(newT(*(rhs.ptr))){DataBase.add();}~SmrtPtr<T>(){delete ptr; ptr=0; DataBase.sub();}void operator=(T val){*ptr=val;}SmrtPtr<T>& operator=(const SmrtPtr<T>& rhs){if(this!=&rhs){delete ptr;ptr(rhs.ptr);}else return this;}int status(){DataBase.status();}T& operator*()const{return *ptr;}T* operator->()const{return ptr;}private:SmrtPtrDB DataBase;T* ptr;}; I''m very confused. Am I implementing this right? Thanks!!!! 解决方案 This is our current counted pointer code: ////counted_ptr - simple reference counted pointer.////The is a non-intrusive implementation that allocates an additional//int and pointer for every counted object. #ifndef __COUNTED_PTR_H#define __COUNTED_PTR_H template <class Xclass counted_ptr{public:typedef X element_type; explicit counted_ptr(X* p = 0) // allocate a new counter: itsCounter(0) {if (p) itsCounter = new counter(p);}~counted_ptr(){release();}counted_ptr(const counted_ptr& r) throw(){acquire(r.itsCounter);}counted_ptr& operator=(const counted_ptr& r){if (this != &r) {release();acquire(r.itsCounter);}return *this;} template <class Y>counted_ptr(const counted_ptr<Y>& r) throw(){acquire(r.itsCounter);}template <class Y>counted_ptr& operator=(const counted_ptr<Y>& r){if (this != &r) {release();acquire(r.itsCounter);}return *this;} X& operator*() const throw() {return *itsCounter->ptr;}X* operator->() const throw() {return itsCounter->ptr;}X* get() const throw() {return itsCounter ?itsCounter->ptr : 0;}bool unique() const throw(){return (itsCounter ? itsCounter->count == 1 : true);} private: struct counter {counter(X* p = 0, unsigned c = 1) : ptr(p), count(c) {}X* ptr;unsigned count;}* itsCounter; void acquire(counter* c) throw(){ // increment the countitsCounter = c;if (c) ++c->count;} void release(){ // decrement the count, delete if it is 0if (itsCounter) {if (--itsCounter->count == 0) {delete itsCounter->ptr;delete itsCounter;}itsCounter = 0;}}}; #endif It''s very hard to understand what it is you''re trying to do. Could you write a snippet of code that describes more about what you''retrying to achieve ? More like a test case for your smart pointer ... It''s very hard to understand what it is you''re trying to do.Could you write a snippet of code that describes more about what you''retrying to achieve ? More like a test case for your smart pointer ...Actually here''s the code; it achieves reference counting (I hope!!!) SmrtPtrDB.hpp #pragma once class SmrtPtrDB{public:SmrtPtrDB():num(1){}SmrtPtrDB(const SmrtPtrDB& rhs):num(rhs.num){}~SmrtPtrDB(){}void add(){num++;}void sub(){num--;}int status(){return num;}private:int num;}; SmrtPtr.hpp template<class T>class SmrtPtr{public:SmrtPtr<T>(T* obj=0):ptr(obj){}SmrtPtr<T>(const SmrtPtr<T>& rhs):ptr(rhs.ptr){DataBase.add();}~SmrtPtr<T>(){DataBase.sub();if(DataBase.status()==0){delete ptr;cout << "Deleted.";}else cout << "Out of scope. " << endl;}void operator=(T val){*ptr=val;}void operator=(T* val){ptr=val;}SmrtPtr<T>& operator=(const SmrtPtr<T>& rhs){if(this!=&rhs){delete ptr;ptr(rhs.ptr);}else return this;}int status(){return DataBase.status();}T& operator*()const{return *ptr;}T* operator->()const{return ptr;}operator T*()const{return ptr;}private:SmrtPtrDB DataBase;T* ptr;}; main.cpp #include <iostream>#include <cstdlib>#include "SmrtPtr.hpp"using namespace std; int main(){{SmrtPtr<intptr(new int);ptr=5;cout << *ptr << endl;{SmrtPtr<intptr2(ptr);ptr2=6;cout << *ptr << endl;cout << *ptr2 << endl;}cout << *ptr << endl;}system("PAUSE");return EXIT_SUCCESS;}It works apparently. Could you help me improve? Thanks!!!! 这篇关于参考计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-22 19:52