C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Reference Counting

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Protoman
Guest





PostPosted: Mon Jul 10, 2006 9:10 am    Post subject: Reference Counting Reply with quote



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()Wink
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





PostPosted: Sat Jul 15, 2006 8:14 pm    Post subject: Re: Reference Counting Reply with quote



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





PostPosted: Mon Jul 17, 2006 7:28 am    Post subject: Re: Reference Counting Reply with quote



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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.