Re: how to pass smart pointes to function.
From: lokb (lokeshbabu_ms_at_rediffmail.com)
Date: 07/08/04
- Next message: Phlip: "Re: Friend a good idea here?"
- Previous message: Arijit: "Re: Is there anything in C++ akin to Java's class Object?"
- In reply to: Phlip: "Re: how to pass smart pointes to function."
- Next in thread: John Harrison: "Re: how to pass smart pointes to function."
- Reply: John Harrison: "Re: how to pass smart pointes to function."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 08 Jul 2004 16:38:47 -0400
Hi,
This is what ObjVar is doing
#ifndef SPTR_HPP
#define SPTR_HPP
#include <string>
using namespace std;
template <class T> class Counted;
template <class T>
class ObjVar
{
public:
ObjVar()
{
m_pCounted = 0;
}
ObjVar(T* pT)
{
m_pCounted = new Counted<T>(pT);
m_pCounted->GetRef();
}
~ObjVar()
{
UnBind();
}
ObjVar(const ObjVar<T>& rVar)
{
m_pCounted = rVar.m_pCounted;
if (!Null())
m_pCounted->GetRef();
}
ObjVar<T>& operator=(const ObjVar<T>& rVar)
{
if (!rVar.Null())
rVar.m_pCounted->GetRef();
UnBind();
m_pCounted = rVar.m_pCounted;
return *this;
}
ObjVar<T>& operator=(Counted<T>* pCounted)
{
if (!rVar.Null())
rVar.m_pCounted->GetRef();
UnBind();
m_pCounted = pCounted;
return *this;
}
T* operator->()
{
if (!Null())
return m_pCounted->my_pT;
}
T& operator*()
{
if (!Null())
return *m_pCounted->my_pT;
}
const T* operator->() const
{
if (Null())
throw return;
return m_pCounted->my_pT;
}
bool Null() const
{
return m_pCounted == 0;
}
void SetNull()
{
UnBind();
}
private:
void UnBind()
{
if (!Null() && m_pCounted->FreeRef() == 0)
delete m_pCounted;
m_pCounted = 0;
}
Counted<T>* m_pCounted;
};
template <class T> class Counted
{
friend class ObjVar<T>;
private:
Counted(T* pT) : Count(0), my_pT(pT)
{
}
~Counted()
{
if (Count == 0)
delete my_pT;
}
unsigned GetRef()
{
return ++Count;
}
unsigned FreeRef()
{
if (Count!=0)
return --Count;
}
T* const my_pT;
unsigned Count;
};
The IELStruct name was misleading sorry abt that.. its actually
IndexStructure and is defined as
typedef ObjVar<IndexStruct> IndexStruct_sptr;
let me know if u require more information
Thanks,
Lokesh
- Next message: Phlip: "Re: Friend a good idea here?"
- Previous message: Arijit: "Re: Is there anything in C++ akin to Java's class Object?"
- In reply to: Phlip: "Re: how to pass smart pointes to function."
- Next in thread: John Harrison: "Re: how to pass smart pointes to function."
- Reply: John Harrison: "Re: how to pass smart pointes to function."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|