 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Protoman Guest
|
Posted: Mon Jul 10, 2006 9:10 am Post subject: Reference Counting |
|
|
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 SmrtPtr
know 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(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)
{
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!!!! |
|
| Back to top |
|
 |
Protoman Guest
|
Posted: Sat Jul 15, 2006 8:14 pm Post subject: Re: Reference Counting |
|
|
Thomas J. Gritzan wrote:
| Quote: | Protoman schrieb:
For swap(), how about *this=rhs? Will that work?
No, swap() _swaps_ the contents, *this=rhs assigns the rhs to the lhs
(lhs gets overridden).
Here's the stuff I fixed:
~SmrtPtr<T>()
{
DataBase->sub();
if(DataBase->status()==0)
{
delete ptr;
delete DataBase;
cout << "Deleted." << endl;
}
else
{
delete DataBase;
Really, think about what you are doing here. Ask yourself this questions:
- Who is the owner of the object pointed to by ptr? Who may delete it
and when?
- Who owns DataBase, who deletes it?
Sorry about my indenting, it looks normal on my screen.
Try another newsreader, or try spaces instead of tabs.
--
Thomas
|
So, how do I fix it? |
|
| Back to top |
|
 |
Protoman Guest
|
Posted: Mon Jul 17, 2006 7:28 am Post subject: Re: Reference Counting |
|
|
Protoman wrote:
| Quote: | Thomas J. Gritzan wrote:
Protoman schrieb:
For swap(), how about *this=rhs? Will that work?
No, swap() _swaps_ the contents, *this=rhs assigns the rhs to the lhs
(lhs gets overridden).
Here's the stuff I fixed:
~SmrtPtr<T>()
{
DataBase->sub();
if(DataBase->status()==0)
{
delete ptr;
delete DataBase;
cout << "Deleted." << endl;
}
else
{
delete DataBase;
Really, think about what you are doing here. Ask yourself this questions:
- Who is the owner of the object pointed to by ptr? Who may delete it
and when?
- Who owns DataBase, who deletes it?
Sorry about my indenting, it looks normal on my screen.
Try another newsreader, or try spaces instead of tabs.
--
Thomas
So, how do I fix it?
|
OK, I fixed the above, but, now I'm wondering: is SmrtPtr<T> safe to
use in a container, such as a linked list? Is it thread safe? |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|