fix pbs with empty object

This commit is contained in:
dockes 2006-12-05 15:16:53 +00:00
parent 5c8d9bc272
commit 42dcdc51cd

View File

@ -5,13 +5,23 @@
template <class X> class RefCntr { template <class X> class RefCntr {
X *rep; X *rep;
int *pcount; int *pcount;
public: public:
X * operator->() {return rep;} RefCntr()
RefCntr() : rep(0), pcount(new int(1)) {} : rep(0), pcount(0)
RefCntr(X *pp) : rep(pp), pcount(new int(1)) {} {}
RefCntr(const RefCntr &r) :rep(r.rep), pcount(r.pcount) { (*pcount)++;} RefCntr(X *pp)
RefCntr& operator=(const RefCntr& r) { : rep(pp), pcount(new int(1))
if (rep == r.rep) return *this; {}
RefCntr(const RefCntr &r)
: rep(r.rep), pcount(r.pcount)
{
if (pcount)
(*pcount)++;
}
RefCntr& operator=(const RefCntr& r)
{
if (rep == r.rep)
return *this;
if (pcount && --(*pcount) == 0) { if (pcount && --(*pcount) == 0) {
delete rep; delete rep;
delete pcount; delete pcount;
@ -22,9 +32,16 @@ template <class X> class RefCntr {
(*pcount)++; (*pcount)++;
return *this; return *this;
} }
~RefCntr() {if (--(*pcount) == 0) {delete rep;delete pcount;}} ~RefCntr()
int getcnt() const {return *pcount;} {
const X * getptr() const {return rep;} if (pcount && --(*pcount) == 0) {
delete rep;
delete pcount;
}
}
X *operator->() {return rep;}
int getcnt() const {return pcount ? *pcount : 0;}
const X *getptr() const {return rep;}
}; };