Skip to content

Factory class doesn't allow derived classes to be created #1120

Closed
@hughed2

Description

@hughed2

I'm trying to bind a factory class which takes in a constructor function and can later use that function to create an instance of the desired object. This should be working with inherited classes as well. In C++ it works fine, but in Python it looks like I'm losing the derived type. The derived class is created, deleted, and then an instance of the base class is returned.

Here are example bindings, keeping it as close to our original code as possible.
example.cpp

And then some sample python showing where it doesn't work as I expect
import example

def create_dog():
  class Dog(example.Animal):
    def _init__(self):
      example.Animal.__init__(self)
    def speak(self):
      print "Woof!"
    def __del__(self):
      print "The puppy is moving to a farm upstate"
  return Dog

in_python = create_dog()()
in_python.speak()
# Expected output: Woof!
# Actual output: Woof!

example.set_factory(create_dog())
in_cpp = example.get_animal()
# Expected output: Making new animal from factory
# Actual output: Making new animal from factory
#                The puppy is moving to a farm upstate
in_cpp.speak()
# Expected output: Woof!
# Actual output: I am just a generic animal, I have no voice

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions