Garbage Container in C++

Started by
29 comments, last by stefu 22 years ago






"How did you get that conversion to work?"
Don''t actually know, but it works
I don''t really understand all the casting operations.

Also these work:
pDesktop->add((Component *)b);
pDesktop->add((Button *)b);

Btw. Here''s my current SmartPointer file. It''s quite similar to the one in gamedev.net article.


  class SmartObject{public:	int m_refcnt;	void addRef() { m_refcnt++; }	bool delRef() { return (--m_refcnt <= 0); }public:	SmartObject() : m_refcnt(0){}	virtual ~SmartObject(){}};template <class T> class SmartPointer{protected:	SmartObject *m_ptr;	void addRef() { if(m_ptr) m_ptr->addRef(); }	void delRef() {		if( m_ptr ) { if(m_ptr->delRef()) delete m_ptr; m_ptr=0; }	}	void assign(SmartObject *ptr) {	delRef(); m_ptr = ptr; addRef(); }public:	SmartPointer(T *ptr=0) : m_ptr(ptr)					{ addRef(); }	SmartPointer(const SmartPointer& p) :m_ptr(p.m_ptr) { addRef(); }	~SmartPointer() 									{ delRef(); }	SmartPointer& operator = ( T* ptr ) 			{ assign(ptr); return *this; }	SmartPointer& operator = ( SmartPointer& ptr ) 	{ assign(ptr); return *this; }	T& operator *  () const	{ return *((T *)m_ptr); }	T* operator -> () const	{ return (T *)m_ptr; }	operator T* () 		{ return (T *)m_ptr; }	operator const T* (){ return (T *)m_ptr; }	operator bool () 	{ return m_ptr!=0; }	bool operator == (T *ptr) 					{ return ptr == m_ptr; }	bool operator == (const SmartPointer& ptr) 	{ return ptr.m_ptr == this->m_ptr; }	bool operator != (T *ptr) 					{ return ptr != m_ptr; }	bool operator != (SmartPointer& ptr) 		{ return ptr.m_ptr != this->m_ptr; }};  

This topic is closed to new replies.

Advertisement