Boost logo

Boost :

From: John Hunter (jdhunter_at_[hidden])
Date: 2001-08-17 13:11:25


I have some classes that I want to use polymorphically like this:

class Base;
typedef boost::shared_ptr<Base> SmartPtrBase;

class Base {
public:
  virtual ~Base() {};
  virtual void sayit() const=0;
};

class Foo : public Base {
public:
  void sayit() const { cout << "Foo" << endl; }
};

class Bar {
public:
  Bar( SmartPtrBase p) : pB(p) {}
  void says() const { pB->sayit(); }
private:
  SmartPtrBase pB;
};

The problem I am having is if I construct a Foo in python, I don't
know how to pass it to Bar in the constructor. Is the only way to
make Base declare a clone method that returns a SmartPtrBase, eg

  virtual SmartPtrBase clone() =0;

and then in python do,

f = Foo()
f.sayit()

bar = Bar( f.clone() )
bar.says()

Or is there a less intrusive (ie, one where I don't have to write the
clone methods for all derived classes) or better to take an object
created in python and pass it to a class that is expecting a smart
pointer to that object?

Thanks,
John Hunter


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk