mirror of
https://github.com/pybind/pybind11.git
synced 2026-07-01 03:47:03 +00:00
return best representation of polymorphic types (fixes #105)
This commit is contained in:
24
example/example16.cpp
Normal file
24
example/example16.cpp
Normal file
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
example/example16.cpp -- automatic upcasting for polymorphic types
|
||||
|
||||
Copyright (c) 2015 Wenzel Jakob <wenzel@inf.ethz.ch>
|
||||
|
||||
All rights reserved. Use of this source code is governed by a
|
||||
BSD-style license that can be found in the LICENSE file.
|
||||
*/
|
||||
|
||||
#include "example.h"
|
||||
|
||||
struct BaseClass { virtual ~BaseClass() {} };
|
||||
struct DerivedClass1 : BaseClass { };
|
||||
struct DerivedClass2 : BaseClass { };
|
||||
|
||||
void init_ex16(py::module &m) {
|
||||
py::class_<BaseClass>(m, "BaseClass").def(py::init<>());
|
||||
py::class_<DerivedClass1>(m, "DerivedClass1").def(py::init<>());
|
||||
py::class_<DerivedClass2>(m, "DerivedClass2").def(py::init<>());
|
||||
|
||||
m.def("return_class_1", []() -> BaseClass* { return new DerivedClass1(); });
|
||||
m.def("return_class_2", []() -> BaseClass* { return new DerivedClass2(); });
|
||||
m.def("return_none", []() -> BaseClass* { return nullptr; });
|
||||
}
|
||||
Reference in New Issue
Block a user