[C++-sig] smart pointer and polymorphism problem

Christophe Pradal christophe.pradal at cirad.fr
Thu Nov 27 10:52:23 CET 2003

I use an intrusive refcount pointer as described by Scott Meyers in 
"More effective C++" and improved by adding template casting operator 
(Scott Meyers again, but later).
Considering RefCountObject as the base class for pointed objects and 
RefCountPtr the smart pointer template, I built a small example 
reproducing the error:

class O: public virtual RefCountObject
  O(): RefCountObject() {}
  virtual ~O() {}
  virtual int get() = 0;

class A: public O
  int _a;
  A(): O(), _a(1) {}
  virtual ~A( ) {}
  int get() {return _a;}

typedef RCPtr<O> OPtr;
typedef RCPtr<A> APtr;

int foo(const OPtr& p) { return p->get(); }

  class_< O, OPtr, boost::noncopyable >("O", no_init);
  class_< A, APtr, bases<O>,boost::noncopyable >("A", init<>());
  def( "foo",foo );

In python, the following test fail:

 >>> import test
 >>> a= test.A()
 >>> test.foo(a)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
Boost.Python.ArgumentError: Python argument types in
did not match C++ signature:
    foo(class TOOLS::RefCountPtr<class O>)

Any idea?

Christophe PRADAL

More information about the Cplusplus-sig mailing list