[C++-sig] Automatic downcast question

Stefan Franke franke at ableton.com
Sat Jun 14 21:58:55 CEST 2003

After observing some strange behaviour I've extracted the toy example

We have a simple inheritance relation A < B < C. The classes A and B
are wrapped, C remains unwrapped.

struct A
  virtual std::string classname() { return "A"; }

struct B : A
  virtual std::string classname() { return "B"; }

struct C : B
  virtual std::string classname() { return "C"; }

A* getA()
  static A* pA = new A;
  return pA;

A* getB()
  static A* pA = new B;
  return pA;

A* getC()
  static A* pA = new C;
  return pA;

  def("getA", getA, return_value_policy<reference_existing_object>());
  def("getB", getB, return_value_policy<reference_existing_object>());
  def("getC", getC, return_value_policy<reference_existing_object>());

  class_<A>("A").def("classname", &A::classname);
  class_<B>("B", bases<A>);
  // C remains unwrapped


Now, in Python:

>>> from Test import *
>>> getA()
<Test.A object at 0x...>
>>> getB()
<Test.B object at 0x...>       // <<< (Superb feature, anyway!)
>>> getC()
<Test.A object at 0x...>       // <<< Why not Test.B?


Is there a way to make getC() return a wrapped B object instead of
a wrapped A?

This is important for my intended use of the BPL, since A and B (and
in fact many more) are wrapped API classes, whereas C (and C1, C2, ..)
are client classes that can't be exposed to BPL in advance.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20030614/b8927331/attachment.htm>

More information about the Cplusplus-sig mailing list