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

David Abrahams dave at boost-consulting.com
Tue Dec 24 04:40:38 CET 2002


"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





More information about the Cplusplus-sig mailing list