You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

24 lines
871 B

  1. /*
  2. example/example16.cpp -- automatic upcasting for polymorphic types
  3. Copyright (c) 2015 Wenzel Jakob <wenzel@inf.ethz.ch>
  4. All rights reserved. Use of this source code is governed by a
  5. BSD-style license that can be found in the LICENSE file.
  6. */
  7. #include "example.h"
  8. struct BaseClass { virtual ~BaseClass() {} };
  9. struct DerivedClass1 : BaseClass { };
  10. struct DerivedClass2 : BaseClass { };
  11. void init_ex16(py::module &m) {
  12. py::class_<BaseClass>(m, "BaseClass").def(py::init<>());
  13. py::class_<DerivedClass1>(m, "DerivedClass1").def(py::init<>());
  14. py::class_<DerivedClass2>(m, "DerivedClass2").def(py::init<>());
  15. m.def("return_class_1", []() -> BaseClass* { return new DerivedClass1(); });
  16. m.def("return_class_2", []() -> BaseClass* { return new DerivedClass2(); });
  17. m.def("return_none", []() -> BaseClass* { return nullptr; });
  18. }