[C++-sig] eposing classes with protected destructors

Daniel Paull dlp at fractaltechnologies.com
Tue Dec 24 05:03:50 CET 2002


Ah bugger, I pasted the duff implementation of the ref counting to add a
flavour of completeness but missed the member var.  Add "int m_refCount"
to the class def and away we go!

Dan

> -----Original Message-----
> From: c++-sig-admin at python.org [mailto:c++-sig-admin at python.org] On
Behalf
> Of David Abrahams
> Sent: Tuesday, 24 December 2002 11:41 AM
> To: c++-sig at python.org
> Subject: Re: [C++-sig] eposing classes with protected destructors
> 
> "Daniel Paull" <dlp at fractaltechnologies.com> writes:
> 
> >> Show me a small example, please. I still can't believe it.
> >
> > The code below compiles fine against 1.29.0,
> 
> I still can't believe that.
> 
> > but fails as per my original post when compiled against yesterdays
> > CVS version.
> >
> > ----------------SNIP-------------------------
> > #include <boost/python.hpp>
> > using namespace boost::python;
> >
> > template< class T >
> > class RefCountedObject
> > {
> > public:
> > 	typedef T element_type;
> > 	explicit RefCountedObject( T* t ) : m_pPtr( t )
> > 	{
> > 		if ( m_pPtr ) m_pPtr->addRef();
> > 	}
> > 	~RefCountedObject()
> > 	{
> > 		if ( m_pPtr ) m_pPtr->releaseRef();
> > 	}
> > 	T& operator*() const { return *get(); }
> > 	T* operator->() const { return get(); }
> > 	T* get() const { return m_pPtr; }
> > private:
> > 	T* m_pPtr;
> > };
> >
> > // needed for latest CVS version of boost.python
> > namespace boost { // dang VC
> > template<class T> T * get_pointer( RefCountedObject<T> const& p )
> > {
> >     return p.get();
> > }
> > }
> >
> > class A
> > {
> > public:
> > 	A( bool val = false ) : m_refCount( 0 ) { m_value = val; }
> 
> VC6 sez:
> 
> test.cpp(35) : error C2614: 'A' : illegal member initialization:
> 'm_refCount' is not a base or member
> test.cpp(38) : error C2065: 'm_refCount' : undeclared identifier
> 
> > 	virtual void setValue( bool val ) { m_value = val; }
> > 	virtual bool getValue() const { return m_value; }
> > 	virtual void addRef () { ++m_refCount; }
> > 	virtual void releaseRef () { if( !--m_refCount ) delete this; }
> > 	virtual int getRefCount () { return 0; }
> > protected:
> > 	virtual ~A() {}
> > 	bool m_value;
> > };
> >
> > BOOST_PYTHON_MODULE( foo )
> > {
> > 	class_< A, RefCountedObject< A > >( "A", init< bool >() )
> > 		.def( "setValue", &A::setValue )
> > 		.def( "getValue", &A::getValue )
> > 		;
> > }
> > ----------------SNIP-------------------------
> 
> 
> --
>                        David Abrahams
>    dave at boost-consulting.com * http://www.boost-consulting.com
> Boost support, enhancements, training, and commercial distribution
> 
> 
> _______________________________________________
> C++-sig mailing list
> C++-sig at python.org
> http://mail.python.org/mailman/listinfo/c++-sig





More information about the Cplusplus-sig mailing list