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